我的CheckBoxList是用数据源来绑定的,给 input 加个 事件 无法判断 选中多少个,checkBox,请问我要如何写呢?这样写不对 
CS
   for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
        {
            this.CheckBoxList1.Items[i].Attributes.Add("onclick", "SetCheckBoxState(this);");
        }
源代码
 function SetCheckBoxState(str) {
            o = document.getElementById('CheckBoxList1').getElementsByTagName('input');
            var val = 0;
            for (i = 0; i < o.length; i++) {
                if (o[i].type == "checkbox" && o[i].checked) {
                    val = val + 1;
                }
                if (val > 4) {
                    alert('最多只能选择4项!');
                    str.checked = false;
                    return
                }
            }
        }

解决方案 »

  1.   

        var t=0;
           function total()
           {
                if($("input[type=checkbox]").is(":checked"))
                t++;
           }
      

  2.   

    用FF断点调试,查看o对象是不是拿到了所有的checkbox,没有拿到的话,试试"CheckBoxList应该会有一个name,document.getElementByName('')."如果o对象没有问题,断点看看"if (o[i].type == "checkbox" && o[i].checked) {"这个判断有没有什么问题,有时候,也许需要你这样写:o[i].checked=="checked" || o[i].checked == true.
      

  3.   

    抓狂中,第一次做到试试, 点击checkbox js 提示出错,结果新建一个页面就没有问题了