jQuery Checkbox全部选中 [英] Jquery Checkbox check all

查看:94
本文介绍了jQuery Checkbox全部选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是jQuery专家,但是我尝试为我的应用程序创建一个小脚本.我想选中所有复选框,但无法正常工作.

I am not expert with jQuery but I have tried to create a little script for my application. I want to check all checkboxes but it isn't working correctly.

首先我尝试使用attr,然后尝试使用prop,但是我做错了事.

First I tried to use attr and after that I tried with prop but I'm doing something wrong.

我首先尝试了这个:

$("#checkAll").change(function(){

  if (! $('input:checkbox').is('checked')) {
      $('input:checkbox').attr('checked','checked');
  } else {
      $('input:checkbox').removeAttr('checked');
  }       
});

但这没用.

下一步:这比上面的代码效果更好

Next: This worked better than above code

$("#checkAll").change(function(){

  if (! $('input:checkbox').is('checked')) {
      $('input:checkbox').prop('checked',true);
  } else {
      $('input:checkbox').prop('checked', false);
  }       
});

两个示例都不起作用.我尝试使用Google搜索来寻找答案,但未成功.

Both examples don't work. I tried googling for an answer but was unsuccessful.

jsFiddle: http://jsfiddle.net/hhZfu/4/

jsFiddle: http://jsfiddle.net/hhZfu/4/

谢谢

推荐答案

您需要使用 .prop()进行设置选中的属性

You need to use .prop() to set the checked property

$("#checkAll").click(function(){
    $('input:checkbox').not(this).prop('checked', this.checked);
});

演示:小提琴

这篇关于jQuery Checkbox全部选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆