jQuery 操作Checkbox


1、获取单个checkbox选中项(三种写法)

  1. $("input:checkbox:checked").val()
或者
  1. $("input:[type='checkbox']:checked").val();
或者
  1. $("input:[name='ck']:checked").val();

2、 获取多个checkbox选中项

  1. $('input:checkbox').each(function() {
  2. if ($(this).attr('checked') ==true) {
  3. alert($(this).val());
  4. }
  5. });

3、设置第一个checkbox 为选中值

  1. $('input:checkbox:first').attr("checked",'checked');
或者
  1. $('input:checkbox').eq(0).attr("checked",'true');

4、设置最后一个checkbox为选中值

  1. $('input:radio:last').attr('checked', 'checked');
或者
  1. $('input:radio:last').attr('checked', 'true');

5、根据索引值设置任意一个checkbox为选中值

  1. $('input:checkbox).eq(索引值).attr('checked', 'true');
索引值=0,1,2....
或者
  1. $('input:radio').slice(1,2).attr('checked', 'true');

6、选中多个checkbox同时选中第1个和第2个的checkbox

  1. $('input:radio').slice(0,2).attr('checked','true');

7、根据Value值设置checkbox为选中值

  1. $("input:checkbox[value='1']").attr('checked','true');

8、删除Value=1的checkbox

  1. $("input:checkbox[value='1']").remove();

9、删除第几个checkbox

  1. $("input:checkbox").eq(索引值).remove();
索引值=0,1,2....
如删除第3个checkbox:
  1. $("input:checkbox").eq(2).remove();

10、遍历checkbox

  1. $('input:checkbox').each(function (index, domEle) {
  2. //写入代码
  3. });

11、全部选中

  1. $('input:checkbox').each(function() {
  2. $(this).attr('checked', true);
  3. });

12、全部取消选择

  1. $('input:checkbox').each(function () {
  2. $(this).attr('checked',false);
  3. });

13、是否选中

  1. $('#CheckBox').is(':checked')

14、遍历checkbox用each()方法
  1. $("input[name='box']").each(function(){});
15、设置checkbox被选中用attr();方法
  1. $("input[name='box']").attr("checked","checked");
16、在HTML中,如果一个复选框被选中,对应的标记为 checked="checked"。 但如果用jquery alert($("#id").attr("checked")) 则会提示您是"true"而不是"checked",所以判断 if("checked"==$("#id").attr("checked")) 是错误的,应该是
  1. if(true == $("#id").attr("checked"))
或者

  1. $('#CheckBox').is(':checked')
 17、获取被选中的checkbox的值
  1. $("input[name='box'][checked]").each(function(){
  2. if (true == $(this).attr("checked")) {
  3. alert( $(this).attr('value') );
  4. }
 或者:
  1. $("input[name='box']:checked").each(function(){
  2. if (true == $(this).attr("checked")) {
  3. alert( $(this).attr('value') );
  4. }
  $("input[name='box']:checked")与 $("input[name='box']")有何区别没试过,我试了用 $("input[name='box']")能成功。

  18、获取未选中的checkbox的值

  1. $("input[name='box']").each(function(){
  2. if ($(this).attr('checked') ==false) {
  3. alert($(this).val());
  4. }
  5. });

 19、设置checkbox的value属性的值

  1. $(this).attr("value",值);

20.、 一般都是创建一个js数组来存储遍历checkbox得到的值,创建js数组的方法:

  1. var array= new Array();

往数组添加数据:

  1. array.push($(this).val());

数组以“,”分隔输出:  

  1. alert(array.join(','));

多选框checkbox

  1. $("#chk1").attr("checked",'');//不打勾
  2. $("#chk2").attr("checked",true);//打勾
  3. if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

举了7个不同的checkbox状态

1、全选

  1. $("#btn1").click(function(){
  2. $("input[name='checkbox']").attr("checked","true");
  3. })
2、取消全选(全不选)
  1. $("#btn2").click(function(){
  2. $("input[name='checkbox']").removeAttr("checked");
  3. })
3、选中所有奇数
  1. $("#btn3").click(function(){
  2. $("input[name='checkbox']:odd").attr("checked","true");
  3. })
4、选中所有偶数
  1. $("#btn6").click(function(){
  2. $("input[name='checkbox']:even").attr("checked","true");
  3. })
5、反选
  1. $("#btn4").click(function(){
  2. $("input[name='checkbox']").each(function(){
  3. if($(this).attr("checked"))
  4. {
  5. $(this).removeAttr("checked");
  6. }
  7. else
  8. {
  9. $(this).attr("checked","true");
  10. }
  11. })
  12. })
或者
  1. $("#invert").click(function(){
  2. $("#ruleMessage [name='delModuleID']:checkbox").each(function(i,o){
  3. $(o).attr("checked",!$(o).attr("checked"));
  4. });
  5. });
6、获取选择项的值
  1. var aa="";
  2. $("#btn5").click(function(){
  3. $("input[name='checkbox']:checkbox:checked").each(function(){
  4. aa+=$(this).val()
  5. })
  6. document.write(aa);
  7. })
  8. })
7、遍历选中项
  1. $("input[type=checkbox][checked]").each(function(){
  2. //由于复选框一般选中的是多个,所以可以循环输出
  3. alert($(this).val());
  4. });
运行一下