`

一句话完成全选

阅读更多

  一句话完成全选,取消下面选项中的任何一项,全选前的状态显示取消。

 

 <input type="checkbox" class="chk" id="all_city" name="all_city" value="all" />全选<br/>
  <ul>
 <li><input type="checkbox"  id="pre_city_ids[]" name="pre_city_ids[]"  value="ch" /></li>

<li><input type="checkbox"  id="pre_city_ids[]" name="pre_city_ids[]"  value="fd" /></li>

<li><input type="checkbox"  id="pre_city_ids[]" name="pre_city_ids[]"  value="dh" /></li>

<li><input type="checkbox"  id="pre_city_ids[]" name="pre_city_ids[]"  value="lc" /></li>

<li><input type="checkbox"  id="pre_city_ids[]" name="pre_city_ids[]"  value="po" /></li>

   </ul>

 

$(function () {
    $("input[name='pre_city_ids[]']").click(function () {
        $("input[name='pre_city_ids[]']:checked").length == $("input[name='pre_city_ids[]']").length ? $("#all_city").attr("checked", true) : $("#all_city").attr("checked", false);
    });
    $("#all_city").click(function () {
        $("input[name='pre_city_ids[]']").attr("checked", this.checked);
    });
});

 

    <script language="javascript" type="text/javascript">

        $(function () {
            $("#selAll").click(function () {
                $(".column :checkbox").attr("checked", true);
            });

            $("#unSelAll").click(function () {
                $(".column :checkbox").attr("checked", false);
            });

            $("#reverSel").click(function () {
                //遍历.column 下的 checkbox;
                $(".column :checkbox").each(function () {
                    //给当前勾选的checkbox取反;  其中!$(this).attr("checked")是先获取他的属性,再取反,充当第二个参数;
                    //attr方法只有一个参数时是取值,两个参数时是设值;
                    $(this).attr("checked", !$(this).attr("checked"));
                });
            });
        });

    </script>

 

17	        <input type="checkbox" name="checkItems" id="checkItems" value="全选/全不选"/>全选/全不选
18	        <br>
19	        <input type="checkbox" name="items" value="足球" />足球
20	        <input type="checkbox" name="items" value="篮球"/>篮球
21	        <input type="checkbox" name="items" value="游泳"/>游泳
22	        <input type="checkbox" name="items" value="唱歌"/>唱歌
23	        <br>
24	        <input type="button" name="checkall" id="checkall" value="全选" />
25	        <input type="button" name="checkall" id="checkallNo" value="全不选" />
26	        <input type="button" name="checkall" id="check_revsern" value="反选" />

 

 

01	$(document).ready(function(){
02	    $("#checkall").click(function(){
03	        $("input[name='items']").each(function(){
04	            this.checked = true;
05	        });
06	    });
07	     
08	    $("#checkallNo").click(function(){
09	        $("input[name='items']").each(function(){
10	            this.checked = false;
11	        })
12	    });
13	     
14	    $("#check_revsern").click(function(){
15	        $("input[name='items']").each(function(){
16	            if (this.checked) {
17	                this.checked = false;
18	            }
19	            else {
20	                this.checked = true;
21	            }
22	        });
23	    });
24	     
25	    $("#checkItems").click(function(){
26	        $("input[name='items']").each(function(){
27	            if (this.checked) {
28	                this.checked = false;
29	            }
30	            else {
31	                this.checked = true;
32	            }
33	        });
34	    });
35	});

 

 

分享到:
评论
Global site tag (gtag.js) - Google Analytics