How do I disable ALL checkboxes on a page that are not checked? Do I need to first scan through all the checkboxes or is there a simpler way? I'm still a jQuery newbie
如何禁用未选中的页面上的所有复选框?我是否需要先扫描所有复选框,还是有更简单的方法?我仍然是一个jQuery新手
3 个解决方案
#1
6
Obviously a user won't be able to ever select them again, as you've disabled them...
显然,用户将无法再次选择它们,因为您已禁用它们...
$(':checkbox:not(:checked)').prop('disabled', true);
See the :checkbox
, :not
, and :checked
selector, as well as the prop()
method.
请参阅:复选框,:not,和:checked selector,以及prop()方法。
If you're not using jQuery 1.6.x, then you won't have the prop()
method; so use the attr()
method instead:
如果你没有使用jQuery 1.6.x,那么你将没有prop()方法;所以使用attr()方法代替:
$(':checkbox:not(:checked)').attr('disabled', true);
You can see this working in a jsfiddle that was posted in the comments: http://jsfiddle.net/loktar/LRL2k/1/
您可以在评论中发布的jsfiddle中看到这个:http://jsfiddle.net/loktar/LRL2k/1/
#2
3
This will find all the checkboxes which are not checked
.
这将找到未选中的所有复选框。
$(':checkbox:not(:checked)');
Now you can either use prop
or attr
method to set the disabled
property on the matched checkboxes.
现在,您可以使用prop或attr方法在匹配的复选框上设置disabled属性。
$(':checkbox:not(:checked)').prop('disabled', true);
#3
3
Use checkbox
to get all checkboxes, and then use :not(:checked)
to filter that to the ones that aren't checked (:not
docs, :checked
docs).
使用复选框获取所有复选框,然后使用:not(:checked)将其过滤到未选中的复选框(:not docs,:checked docs)。
$(':checkbox:not(:checked)').attr('disabled', 'disabled');
#1
6
Obviously a user won't be able to ever select them again, as you've disabled them...
显然,用户将无法再次选择它们,因为您已禁用它们...
$(':checkbox:not(:checked)').prop('disabled', true);
See the :checkbox
, :not
, and :checked
selector, as well as the prop()
method.
请参阅:复选框,:not,和:checked selector,以及prop()方法。
If you're not using jQuery 1.6.x, then you won't have the prop()
method; so use the attr()
method instead:
如果你没有使用jQuery 1.6.x,那么你将没有prop()方法;所以使用attr()方法代替:
$(':checkbox:not(:checked)').attr('disabled', true);
You can see this working in a jsfiddle that was posted in the comments: http://jsfiddle.net/loktar/LRL2k/1/
您可以在评论中发布的jsfiddle中看到这个:http://jsfiddle.net/loktar/LRL2k/1/
#2
3
This will find all the checkboxes which are not checked
.
这将找到未选中的所有复选框。
$(':checkbox:not(:checked)');
Now you can either use prop
or attr
method to set the disabled
property on the matched checkboxes.
现在,您可以使用prop或attr方法在匹配的复选框上设置disabled属性。
$(':checkbox:not(:checked)').prop('disabled', true);
#3
3
Use checkbox
to get all checkboxes, and then use :not(:checked)
to filter that to the ones that aren't checked (:not
docs, :checked
docs).
使用复选框获取所有复选框,然后使用:not(:checked)将其过滤到未选中的复选框(:not docs,:checked docs)。
$(':checkbox:not(:checked)').attr('disabled', 'disabled');