|
JavaScript的checkbox框架可以如下使用:
<FORM NAME="list" METHOD="post"> <INPUT name=no TYPE=checkbox> <INPUT name=no TYPE=checkbox> <INPUT name=no TYPE=checkbox> </FORM>
在js中: for(var i=0;i<document.list.no.length;i++){ if(document.list.no[i].checked){ allvalue +=document.list.no[i].value+","; } }
但是当只有一个checkbox时, 上面的代码就会运行错误了.如: <FORM NAME="list" METHOD="post"> <INPUT name=no TYPE=checkbox> </FORM>
这时document.list.no.length document.list.no[0]将会是undefined 因为它现在已经不再是数组了.所以直接取其value就行了.如: document.list.no.value document.list.no.checked
综合上述情况, 只要在js中加以判断就OK了. if(typeof(document.list.no) == "undefined") cbvalue=""; else if(typeof(document.list.no.length)=="undefined"){ if(document.list.no.checked) cbvalue=document.list.no.value; }else{ for(var i=0;i<document.list.no.length;i++){ if(document.list.no[i].checked){ cbvalue +=document.list.no[i].value+","; } } }
|