JS选择html页面中的checkbox实现全选

<!--html-->

<input id="Checkbox2" type="checkbox" onclick="check(this)" />全选 &nbsp;
 <asp:Button ID="btnAddFuJian" runat="server" Text="添加所选附件" OnClick="btnAddFuJian_Click"
                    OnClientClick="return lessthan();" />

<!--JS-->

<script type="text/javascript">
        function check(obj) //全选

        {
        var gv = document.getElementsByTagName("dd");
       for(var j=0;j<gv.length;j++)
       {
        var aa = gv[j].getElementsByTagName("input");

        if (obj.checked) {

            for (var i = 0; i < aa.length; i++) {
                if (aa[i].type == 'checkbox')
                    aa[i].checked = true;
            }
        }
        if (obj.checked == false) {

            for (var i = 0; i < aa.length; i++) {
                if (aa[i].type == 'checkbox')
                    aa[i].checked = false;
            }
        }
        }
    }
   
    function lessthan()
    {
        var k=0;
            var gv = document.getElementsByTagName("dd");//选择包含checkbox的标签
            for(var j=0;j<gv.length;j++)
            {
                var aa = gv[j].getElementsByTagName("input"); //选择dd里面所有的input
                for (var i = 0; i < aa.length; i++)
                {
                    if (aa[i].type == 'checkbox' && aa[i].checked == true) //判断input是不是checkbox
                    k++;
                }
            }
            if(k==0)
            {  
                alert("请选择附件");
                return false;
            }
            else
            {
                return true;
            }
     }
    </script>

----------全选也可以是这个

<script type="text/javascript">
        function SelectAllCheckboxes(spanChk) {

            elm = document.forms[0];

            for (i = 0; i <= elm.length - 1; i++) {

                if (elm[i].type == "checkbox" && elm[i].id != spanChk.id) {

                    if (elm.elements[i].checked != spanChk.checked)

                        elm.elements[i].click();
                }
            }
        }
    </script>

<asp:CheckBox ID="CheckAll" runat="server"  Text="全选" onclick="javascript: SelectAllCheckboxes(this);"  />

posted on 2011-05-19 14:03  Paste  阅读(5094)  评论(0编辑  收藏  举报

导航