一个form中有不定的checkbox,每个checkbox右边对应一个统计按钮,要求是任意勾选后,点统计按钮,会自动统计表单中被勾选的checkbox的值,如进行统计后,alert得到值为1,2,3,4,5……nhtml:[code=HTML]
<script>
function checkform(){//这里的js应该怎么写?}
</script>
<form name="form1" id="form1">
<input type="checkbox" name="t1" id="t1" value="1">
<input type="button" name="count1" value="统计" onclick="checkform">[/
……
<input type="checkbox" name="tn" id="tn" value="n">
<input type="button" name="countn"  value="统计" onclick="checkform">[/
</form>[code]

解决方案 »

  1.   


    <script>
    function checkform(){//这里的js应该怎么写?
    var chks=document.all.tags("input");
    var s="";
    for(var i=0;i<chks.length;i++){
    if(chks[i].type=="checkbox" ){
    if(chks[i].checked){
    s=s+","+chks[i].value;
    }
    }

    }
    if(s!=""){
    s=s.substring(1);
    alert(s);
    }

    }
    </script>
    <form name="form1" id="form1">
    <input type="checkbox" name="t1" id="t1" value="1">
    <input type="button" name="count1" value="统计" onclick="checkform()">
    <input type="checkbox" name="tn" id="tn" value="n">
    <input type="button" name="countn"  value="统计" onclick="checkform()">
    </form>