checkbox的常用操作

声明:如果你想去看具体的例子,请下载压缩包:checkbox-demo.rar就行了!

里面包括checkbox的基本操作的例子,以及一个淘宝的小例子!!



 
 
  1. Checkbox的操作基本上就几种:选中,未选中,全选,取消全选,反选

  2. <body>

  3. <h3>关于checkbox的用法的例子</h3><br/>

  4. <inputid="c1"type="checkbox"name="option1"value="value1"checked="checked"/>Value1

  5. <inputid="c2"type="checkbox"name="option2"value="value2"/>Value2<br/>

  6. <inputid="c3"type="checkbox"name="option3"value="value3"/>Value3  

  7. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<inputtype="button"value="触发事件"/>

  8. </body>

  9. 1.  操作一:判断一个checkbox是否处于选中状态

  10.                //方法一:

  11.                if($("#c1").is(":checked")) {

  12.                    alert("方法一可以用!");

  13.                } else {

  14.                    alert("方法一不可以用");

  15.                }

  16.                //方法二:

  17.                if($("#c2").attr("checked") == false) {

  18.                    alert("方法二可以用!");

  19.                } else {

  20.                    alert("方法二不可以用");

  21.                }

  22. 2.  操作二:让一个checkbox处于选中状态!

  23. $("#c3").attr("checked","checked");

  24. 或者

  25. $("#c3").attr("checked",true);

  26. 3.  操作三:取消一个checkbox的选中状态!

  27. $("#c1").removeAttr("checked");

  28. 或者

  29. $("#c1").attr("checked",false);

  30. 4.  操作四:获取checkbox的值!

  31. var value1 = $("#c1").val();

  32. 或者

  33. Var value2 = document.getElementById(“c2”).value;

  34. <body>

  35. <h3>下面我们来练习一下全选与反选的例子</h3>

  36. <inputclass="son"type="checkbox"name="son"value="value1"/>

  37. <inputclass="son"type="checkbox"name="son"value="value2"/>

  38. <inputclass="son"type="checkbox"name="son"value="value3"/>

  39. <inputclass="son"type="checkbox"name="son"value="value4"/>

  40. <inputclass="son"type="checkbox"name="son"value="value5"/>

  41. <inputclass="son"type="checkbox"name="son"value="value6"/><br/><br/>

  42. <inputid="father"type="checkbox"name="father"value="value7"/>全选

  43.    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  44. <inputid="un"type="checkbox"name="option8"value="value8"/>反选

  45. </body>

  46. 5.  操作五:全选

  47. 方法一:

  48.                $("#father").click(function(){

  49.                    //获取父选框和子选框数组

  50.                    var father = document.getElementById("father");

  51.                    var sons = document.getElementsByName("son");

  52.                    //设置子选框的选中状态和父相同

  53.                    for(var i=0; i <sons.length; i++) {

  54.                        sons[i].checked = father.checked;

  55.                    }

  56.                });

  57. 方法二:

  58.                $("#father").click(function(){

  59.                    var flag = $(this).attr("checked");

  60.                    $("[name=son]:checkbox").each(function(){

  61.                        $(this).attr("checked",flag);

  62.                    });

  63.                });

  64. 6.通过每一个子复选框来判断全选的复选框是否应处于选中状态

  65. 方法一:

  66.                $(".son").click(function(){

  67.                    var num=0;

  68.                    var father = document.getElementById("father");

  69.                    var sons = document.getElementsByName("son");

  70.                    for(var i=0; i <sons.lenth; i++) {

  71.                        if(sons[i].checked) {

  72.                            num++;

  73.                        }

  74.                    }

  75.                    if(num == sons.lenth) {

  76. father.checked = true;

  77.                    } else {

  78. father.checked = false;

  79.                    }

  80.                });

  81. 方法二:

  82.                $(".son").click(function(){

  83.                    $("[name=son]:checkbox").each(function(){

  84.                        if($(this).attr("checked") == false) {

  85.                            $("#father").attr("checked",false);

  86.                        }

  87.                    });

  88.                });

  89. 7.反选:

  90.                $("#un").click(function(){

  91.                    var num = 0;

  92.                    var father = document.getElementById("father");

  93.                    var sons = document.getElementsByName("son");

  94.                    for(var i=0; i<sons.length; i++) {

  95.                        sons[i].checked = !sons[i].checked;

  96.                        if(sons[i].checked) {

  97.                            num++;

  98.                        }

  99.                    }

  100.                    if(num == sons.length) {

  101. father.checked = true;

  102.                    } else {

  103. father.checked = false;

  104.                    }

  105.                });


180521268.jpg

***********************************************************************************

2013-12-05-add

1
2
3
4
5
6
/**
  * 操作五:全选
  */
$( "#father" ).click( function (){
     $( "input[name='son']" ).attr( "checked" ,$( this ).is( ":checked" ));
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
  * 6.通过每一个子复选框来判断全选的复选框是否应处于选中状态
  */
     $( ".son" ).click( function (){
         var  num = 0;
         var  length = $( "input[name='son']" ).length;
         $( "input[name='son']" ).each( function (){
             if ($( this ).is( ":checked" )) {
                 num++;
             }
         });
                                   
         if (num == length) {
             $( "#father" ).attr({ "checked" : "checked" });
         else  {
             $( "#father" ).removeAttr( "checked" );
         }
                                   
     });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
  * 7.反选
  */
     $( "#un" ).click( function (){       
         var  num = 0;
         var  length = $( "input[name='son']" ).length;
         $( "input[name='son']" ).each( function (index,domEle){
             if ($( this ).is( ":checked" )) {
                 $( this ).removeAttr( "checked" );
             else  {
                 $( this ).attr( "checked" , true );
                 num++;
             }
         });
                                 
         if (num == length) {
             $( "#father" ).attr({ "checked" : "checked" });
         else  {
             $( "#father" ).removeAttr( "checked" );
         }
     });

===============================================================

2013-12-10-add

$("input[name='son']:checked").each(function(){

//if($(this).is(":checked")) {

num++;

//}

});




     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/772066,如需转载请自行联系原作者






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值