|
热门文章 |
|
|
|
|
下拉框中的复选框 |
来源:中国站长天空 更新时间:2009/12/3 0:19:35 阅读次数: 我要投稿 |
|
<html> <head> <title>下拉框中的复选框</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <!--把下面代码加到<head>与</head>之间--> <style type="text/css"> body,td{font-size:12px;color:#000000;} .checkbox{width:15px;height:15px;} .cked{margin:1px;padding:2px;width:98%;display:block;background-color:highlight;color:highlighttext;} .nock{margin:1px;padding:2px;width:98%;display:block;} </style> </head> <body> <!--把下面代码加到<body>与</body>之间--> <script language="javascript"> <!-- function HtmlEncode(text){ return text.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>'); } function _checkbox(name,str,defv){ //haiwa@2005-8-17 //http://www.51windows.net var arr=str.split("^"); var ck="",bc=""; for(var i=0;i<arr.length;i++){ var thisarr=arr[i].split("@="); if (thisarr[0].length>0){ var t=(thisarr.length==2)?thisarr[0]:arr[i]; var v=(thisarr.length==2)?thisarr[1]:arr[i]; if((","+HtmlEncode(defv).toLowerCase()+",").replace(/ ,/g, ',').indexOf(","+v+",")!=-1){ck=" checked";cls="cked";} else{ck="";cls="nock";} var thisstr="<label class=\""+cls+"\" for=\"i_"+name+"_"+i+"\" id=\"l_"+name+"_"+i+"\">"; thisstr+="<input class=\"checkbox\" onpropertychange=\"document.getElementById('l_"+name+"_"+i+"').className=(document.getElementById('i_"+name+"_"+i+"').checked)?'cked':'nock';\" onclick=\"document.getElementById('l_"+name+"_"+i+"').className=(document.getElementById('i_"+name+"_"+i+"').checked)?'cked':'nock';\" type=\"checkbox\""+ck+" name=\""+name+"\" id=\"i_"+name+"_"+i+"\" value=\""+HtmlEncode(v)+"\" \/> "; thisstr+=HtmlEncode(t)+"</label>"; document.write(thisstr); } } } function _getv(o){ var allvalue=""; if(typeof(o)=="undefined"){return "";} if (typeof(o.length)=="undefined"){ if(o.checked){return o.value+ ",";}else{return "";} } for(var i=0;i<o.length;i++){ if(o[i].checked){ allvalue +=o[i].value+","; } } return allvalue; } function _setv(o,defv){ var allvalue=(","+HtmlEncode(defv).toLowerCase()+",").replace(/ ,/g, ','); for(var i=0;i<o.length;i++){ var v = o[i].value; o[i].checked=(allvalue.indexOf(","+v+",")!=-1) } return allvalue; } function _sl(o,b){ for(var i=0;i<o.length;i++){ o[i].checked = b //if(o[i].checked!=b){o[i].click();} } } //--> </script> <form method="post" name="myform" action="?"> <table border="0" width="200"> <tr> <td><div style="width:180px;height:150px;overflow:auto;border: 2px inset #FFFFFF;"> <script language="javascript"> <!-- _checkbox("city","北京^河北^黑龙江^河南^江西^辽宁^宁夏^四川^天津^浙江^香港","北京,山东") //--> </script> </div></td> </tr> <tr> <td><button onclick='_sl(document.myform.city,true);'>全选</button> <button onclick='_sl(document.myform.city,false);'>全不选</button> <button onclick='_setv(document.myform.city,"甘肃,广东");'>set值</button> <button onclick='alert(_getv(document.myform.city));'>get值</button></td> </tr> <tr> <td><div style="width:180px;height:120px;overflow:auto;border: 2px inset #FFFFFF;"> <script language="javascript"> <!-- _checkbox("WebSite","Google.com@=http://www.google.com^Baidu.com@=http://www.baidu.com^Zzsky.cn@=http://www.zzsky.cn","http://www.zzsky.cn") //--> </script> </div></td> </tr> <tr> <td><button onclick='_sl(document.myform.WebSite,true);'>全选</button> <button onclick='_sl(document.myform.WebSite,false);'>全不选</button> <button onclick='_setv(document.myform.WebSite,"http://www.google.com");'>set值</button> <button onclick='alert(_getv(document.myform.WebSite));'>get值</button></td> </tr> <tr> <td align="center"><button onclick='document.myform.reset();'>重置</button> </td> </tr> </table> </form> </body> </html> |
|
上一篇文章: 文本框自动匹配效果下一篇文章: 判断用户密码强度 |
|
|