jQuery:除了当前的复选框之外,禁用所有复选框 [英] jQuery: Disable all checkboxes apart from current one
问题描述
我有复选框,需要采取类似的方式与单选按钮控件。基本上当一个检查所有其他需要取消选中。我该如何以尽可能少的痛苦实现这一点?
I have checkboxes that need to act in a similar fashion to radio button controls. Essentially when one is checked all others need to be unchecked. How can I achieve this with as little pain as possible?
所以总结一下。如果选中了复选框,则必须取消选中所有其他(兄弟),而保留点击的一个复选框。
So to summarise. If a checkbox is checked, all others (siblings) must then be unchecked leaving the clicked one checkstate untouched.
我已经知道如何取消选中所有复选框, ,我必须首先存储检查的复选框的选中状态,然后在取消选中所有复选框后重新应用。我想知道是否有一个方法可以用一些花哨的jQuery选择器或一些这样做。
I already know how to uncheck all checkboxes but if I did this, I would have to first store the checked state of the checkbox that was checked, then reapply it after unchecking all checkboxes. I wondered if there was a way of doing this with some fancy jQuery selectors or some such.
推荐答案
。很明显,单选按钮是有原因的。人们期望单选按钮是1的n选择,并且复选框是0到n的选择。
By using radio buttons. Seriously, radio buttons are there for a reason. People expect radio buttons to be a "1 out of n" selection and checkboxes to be a "0 up to n" selection.
无论如何, p>
Anyway, here's the code:
$('input:checkbox').click(function() {
$(this).siblings('input:checkbox').removeAttr('checked');
});
演示: http://jsfiddle.net/ThiefMaster/AVEjt/
但请仅在实际需要允许用户取消选中所有内容时才使用它。那么它是可以接受的 - 否则单选按钮是更好。
But please, only use it if you actually need to allow the user to uncheck everything. Then it's acceptable - otherwise radio buttons are much better.
这篇关于jQuery:除了当前的复选框之外,禁用所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!