javascript常用函数
打开窗口 gridview操作 字符串检验
Code
//改变排序方式(asc<-->desc)
function chgSortOrder(lastOrder){
var newOrder = "";
if(lastOrder != ""){
if(lastOrder=="asc"){
newOrder = "desc";
}else if(lastOrder=="desc"){
newOrder = "asc";
}
}else{ //first time order is asc(default)
newOrder = "asc";
}
return newOrder;
}
//检查字符的长度,按汉字的长度计算
function ChineseLenLimit( str, maxLen){
var Strs = str;
var strlength=0;
var i;
for ( i=0;i<str.length;i++) {
if(str.charCodeAt(i)>=1000)
strlength += 2;
else
strlength += 1;
}
if ( strlength > maxLen ){
return false;
}
return true;
}
function isEmail(theStr){
var atIndex = theStr.indexOf('@');
var dotIndex = theStr.indexOf('.',atIndex);
var flag = true;
var theSub = theStr.substring(0,dotIndex+1);
if((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
{ flag = false; }
else { flag =true; }
return(flag);
}
function openwindow(url,name,iWidth,iHeight)
{
var url; //转向网页的地址;
var name; //网页名称,可为空;
var iWidth; //弹出窗口的宽度;
var iHeight; //弹出窗口的高度;
var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;
var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;
window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=yes,location=no,status=yes');
//window.open(url,name,'height='+iHeight+',width='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=yes,resizeable=yes,location=no,status=yes');
}
function ___doPostBack(eventTarget, eventArgument)
{
var theform = document.forms['form1'];
if (!theform)
{
theform = document.form1;
}
theform.__EVENTTARGET.value = eventTarget ;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
function openWindows(url, width, heigth)
{
res=window.showModalDialog(url,window,"status:false;resizable:yes;dialogWidth:"+width+"px;dialogHeight:"+heigth+"px");
___doPostBack('Form1',res);
}
function SelectCheck(btn,gv)
{
var shouldSelect;
if(btn.tag == 0)
{
btn.value = " 反选";
btn.tag = 1;
shouldSelect = true;
}
else
{
btn.value = " 全选";
btn.tag = 0;
shouldSelect = false;
}
var TargetChildControl = "chkBox1";
var Inputs = gv.getElementsByTagName("INPUT");
for(var n = 0; n < Inputs.length; ++n)
{
if(Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl,0) >= 0)
{
Inputs[n].checked=shouldSelect;
}
}
}
function CheckBox(gv,TargetChildControl)
{
var Inputs = gv.getElementsByTagName("INPUT");
for(var n = 0; n < Inputs.length; ++n)
{
if(Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl,0) >= 0&&Inputs[n].checked==true)
{
return confirm('是否删除?');
break;
}
}
alert("请至少选择一项");
return false;
}
//treeview的单击事件
function GoClick()
{
var obj = event.srcElement;
if(obj.tagName.toUpperCase() != "INPUT") return;
var tb = obj.parentElement.parentElement.parentElement.parentElement;//当前节点的Table
SelAllChildren(tb,obj.checked);
tb= tb.parentElement;//
while(true)
{
tb = tb.previousSibling
if( tb == null) return;
SetParentCheckBoxStatus(tb);
tb= tb.parentElement
if( tb == null) return;
}
}
function SelAllChildren(tb,checked)
{
var div = tb.nextSibling;
try
{
for(var i=0;i<div.children.length;i++)
{
if(div.children[i].tagName.toUpperCase()!="TABLE")continue;
var chkBox= FindRowCheckBox(div.children[i].rows[0]);
if(chkBox == null) continue;
chkBox.checked = checked;
SelAllChildren(div.children[i],checked);
}
}catch(e){}
}
function SetParentCheckBoxStatus(objTb)
{
var div = objTb.nextSibling;
var bSelAll = true;
var bSelOne = false;
for(var i=0;i<div.children.length;i++)
{
if(div.children[i].tagName.toUpperCase()!="TABLE")continue;
var chkBox= FindRowCheckBox(div.children[i].rows[0]);
if(!chkBox.checked)
{
bSelAll =false;
}else
{
bSelOne = true;
}
}
if(objTb.rows!= null)
{
var chkBox = FindRowCheckBox(objTb.rows[0])
chkBox.checked = bSelOne;
}
}
function FindRowCheckBox(tr)
{
for(var i=0;i<tr.cells.length;i++)
{
if(tr.cells[i].firstChild.tagName == "INPUT")
{
return tr.cells[i].firstChild;
}
}
}
//结束
//改变排序方式(asc<-->desc)
function chgSortOrder(lastOrder){
var newOrder = "";
if(lastOrder != ""){
if(lastOrder=="asc"){
newOrder = "desc";
}else if(lastOrder=="desc"){
newOrder = "asc";
}
}else{ //first time order is asc(default)
newOrder = "asc";
}
return newOrder;
}
//检查字符的长度,按汉字的长度计算
function ChineseLenLimit( str, maxLen){
var Strs = str;
var strlength=0;
var i;
for ( i=0;i<str.length;i++) {
if(str.charCodeAt(i)>=1000)
strlength += 2;
else
strlength += 1;
}
if ( strlength > maxLen ){
return false;
}
return true;
}
function isEmail(theStr){
var atIndex = theStr.indexOf('@');
var dotIndex = theStr.indexOf('.',atIndex);
var flag = true;
var theSub = theStr.substring(0,dotIndex+1);
if((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
{ flag = false; }
else { flag =true; }
return(flag);
}
function openwindow(url,name,iWidth,iHeight)
{
var url; //转向网页的地址;
var name; //网页名称,可为空;
var iWidth; //弹出窗口的宽度;
var iHeight; //弹出窗口的高度;
var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;
var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;
window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=yes,location=no,status=yes');
//window.open(url,name,'height='+iHeight+',width='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=yes,resizeable=yes,location=no,status=yes');
}
function ___doPostBack(eventTarget, eventArgument)
{
var theform = document.forms['form1'];
if (!theform)
{
theform = document.form1;
}
theform.__EVENTTARGET.value = eventTarget ;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
function openWindows(url, width, heigth)
{
res=window.showModalDialog(url,window,"status:false;resizable:yes;dialogWidth:"+width+"px;dialogHeight:"+heigth+"px");
___doPostBack('Form1',res);
}
function SelectCheck(btn,gv)
{
var shouldSelect;
if(btn.tag == 0)
{
btn.value = " 反选";
btn.tag = 1;
shouldSelect = true;
}
else
{
btn.value = " 全选";
btn.tag = 0;
shouldSelect = false;
}
var TargetChildControl = "chkBox1";
var Inputs = gv.getElementsByTagName("INPUT");
for(var n = 0; n < Inputs.length; ++n)
{
if(Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl,0) >= 0)
{
Inputs[n].checked=shouldSelect;
}
}
}
function CheckBox(gv,TargetChildControl)
{
var Inputs = gv.getElementsByTagName("INPUT");
for(var n = 0; n < Inputs.length; ++n)
{
if(Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl,0) >= 0&&Inputs[n].checked==true)
{
return confirm('是否删除?');
break;
}
}
alert("请至少选择一项");
return false;
}
//treeview的单击事件
function GoClick()
{
var obj = event.srcElement;
if(obj.tagName.toUpperCase() != "INPUT") return;
var tb = obj.parentElement.parentElement.parentElement.parentElement;//当前节点的Table
SelAllChildren(tb,obj.checked);
tb= tb.parentElement;//
while(true)
{
tb = tb.previousSibling
if( tb == null) return;
SetParentCheckBoxStatus(tb);
tb= tb.parentElement
if( tb == null) return;
}
}
function SelAllChildren(tb,checked)
{
var div = tb.nextSibling;
try
{
for(var i=0;i<div.children.length;i++)
{
if(div.children[i].tagName.toUpperCase()!="TABLE")continue;
var chkBox= FindRowCheckBox(div.children[i].rows[0]);
if(chkBox == null) continue;
chkBox.checked = checked;
SelAllChildren(div.children[i],checked);
}
}catch(e){}
}
function SetParentCheckBoxStatus(objTb)
{
var div = objTb.nextSibling;
var bSelAll = true;
var bSelOne = false;
for(var i=0;i<div.children.length;i++)
{
if(div.children[i].tagName.toUpperCase()!="TABLE")continue;
var chkBox= FindRowCheckBox(div.children[i].rows[0]);
if(!chkBox.checked)
{
bSelAll =false;
}else
{
bSelOne = true;
}
}
if(objTb.rows!= null)
{
var chkBox = FindRowCheckBox(objTb.rows[0])
chkBox.checked = bSelOne;
}
}
function FindRowCheckBox(tr)
{
for(var i=0;i<tr.cells.length;i++)
{
if(tr.cells[i].firstChild.tagName == "INPUT")
{
return tr.cells[i].firstChild;
}
}
}
//结束