Javascript 取消选中所有其他复选框
当用户单击复选框时,我需要清除所有其他复选框。与单选按钮的行为几乎相同。用户单击复选框“A”,复选框“B”和“C”均未选中。我正在使用jquery,但我不知道如何实现这一点。有什么想法吗 以下是复选框的设置方式:Javascript 取消选中所有其他复选框,javascript,jquery,html,css,Javascript,Jquery,Html,Css,当用户单击复选框时,我需要清除所有其他复选框。与单选按钮的行为几乎相同。用户单击复选框“A”,复选框“B”和“C”均未选中。我正在使用jquery,但我不知道如何实现这一点。有什么想法吗 以下是复选框的设置方式: <div class="sales_block_one"> <span class="sales_box_option_set"><input type="checkbox" value="1" id="exopt10i11553501716" n
<div class="sales_block_one">
<span class="sales_box_option_set"><input type="checkbox" value="1" id="exopt10i11553501716" name="optset[0][id]" /><label for="exopt10i11553501716">Test + £100.00</label></span>
<span class="sales_box_option_set"><input type="checkbox" value="2" id="exopt11i21553501716" name="optset[1][id]" /><label for="exopt11i21553501716">Test + £ 200.00</label></span>
<span class="sales_box_option_set"><input type="checkbox" value="3" id="exopt12i31553501716" name="optset[2][id]" /><label for="exopt12i31553501716">Test 3 + £400.00</label></span>
</div>
测试+英镑;100
测试+英镑;200
测试3+磅;400
如果所有复选框都是同级的,则如下所示
$(':checkbox').change(function(){
if (this.checked) {
$(this).siblings(':checkbox').attr('checked',false);
}
});
好吧,如果你真的想复制单选按钮的行为,就这么简单
$(':checkbox').change(function(){
this.checked = true;
$(this).siblings(':checkbox').attr('checked',false);
});
同级是指元素有一个相同的父级/容器 样品
<div>
<input type="checkbox" /><label>A</label>
<input type="checkbox" /><label>B</label>
<input type="checkbox" /><label>C</label>
<input type="checkbox" /><label>D</label>
</div>
为什么不使用单选按钮?这是一个简单的方法。@klox-也许OP想要支票(☑ ) 关于设计;)是的,我知道单选按钮会很理想,但它们不能用于应用程序。为什么会被否决?我的意思是,这是一个奇怪的用例(除非它像“不属于上述”或其他什么东西……但从文本来看,他意识到他在某种程度上复制了无线电波盒的行为;单选按钮行为。但是,当你勾选/取消勾选B和C(其他复选框)时,行为是什么?愚蠢的问题:什么构成兄弟姐妹?我用谷歌搜索没用。啊,我明白了-在没有兄弟姐妹的情况下有没有办法做到这一点?我在我的OPI中添加了HTML设置,添加了另一个
疯狂的演示
rrrrr…;)这不会完全按照预期工作,因为当您单击取消选中任何框时,它会导致选中所有其他框。
$('.sales_block_one :checkbox').change(function() {
var $self = this; // save the current object - checkbox that was clicked.
$self.checked = true; // make the clicked checkbox checked no matter what.
$($self).closest('span') // find the closest parent span...
.siblings('span.sales_box_option_set') // get the siblings of the span
.find(':checkbox').attr('checked',false); // then find the checbox inside each span and then uncheck it.
});
$("#checkboxA").click(function() {
var checkedStatus = this.checked;
$("#checkboxB_And_C_Selector").each(function() {
this.checked = !checkedStatus;
});
});