Javascript/JQuery设置复选框的值,同时禁用其他复选框
Javascript/JQuery setting value of checkbox while disabling others
我正在想办法。我有几个复选框,采用以下形式
<div class="checkbox">
<input name="someName1" class="checkbox styled" id="checkbox1" onclick="document.controller.setValue('/@someName1', this.checked ? '1' : '', 'someName1');" type="checkbox" value="" />
<label for="checkbox1"> SomeName 1 </label>
</div>
<div class="checkbox">
<input name="someName2" class="checkbox styled" id="checkbox2" onclick="document.controller.setValue('/@someName2', this.checked ? '1' : '', 'someName2');" type="checkbox" value="" />
<label for="checkbox2"> SomeName 2 </label>
</div>
控制器需要在我正在使用的某个系统中设置值。现在以上似乎都起作用了,如果我检查两者,它们都会记录在我的系统中。如果我选中一个,然后取消选中它,它就不会被记录。
问题来了,因为我只想让他们能够选择一个选项(我知道单选按钮是这样的,但我需要使用复选框)。
所以我有以下
$('input.styled').on('change', function() {
$('input.styled').not(this).prop('checked', false);
});
这就确保了只有一个被选中。但是,由于onclick事件,如果我选中了一个复选框,而选中了另一个复选复选框(然后上面的代码关闭了最初的复选框),那么这两个复选框都会被记录为在我的系统中被选中。
因此,我认为我需要对我的onclick事件做点什么,如果复选框不再被选中,以某种方式将其关闭。
我正在考虑做一些类似以下的事情
$('.checkbox').change(function() {
if($(this).is(":checked")) {
$value = $(this).attr("name");
document.controller.setValue('/@'+$value, this.checked ? '1' : '', 'checkbox');
}
});
因此,如果复选框被选中,请将setValue函数应用于名称属性选中它的复选框。如果未选中,我需要以某种方式扭转这种情况。
我该怎么做这样的事?
感谢
您可以更改此
$('.checkbox').change(function() {
if($(this).is(":checked")) {
$value = $(this).attr("name");
document.controller.setValue('/@'+$value, this.checked ? '1' : '', 'checkbox');
}
});
到这个
$('.checkbox').change(function() {
$(".checkbox:checkbox:not(:checked)").each(function( index ) {
$value = $(this).attr("name");
document.controller.setValue('/@'+$value,'', 'checkbox');
});
if($(this).is(":checked")) {
$value = $(this).attr("name");
document.controller.setValue('/@'+$value, this.checked ? '1' : '', 'checkbox');
}
});
在设置选中项目的值之前,您将重置所有未选中的项目。
我认为这就是您所需要的
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
checkbox2radio('.checkbox');
checkbox2radio('.hamlet');
function checkbox2radio(selector) {
$(selector).on('change', function(e) {
var checked = this.checked;
var index = $(selector).index(this);
if (checked) {
$(selector).each(function(i) {
if(i == index) {
}
else {
$(selector).eq(i).removeAttr('checked');
}
});
}
});
}
})
</script>
<input type="checkbox" class="checkbox"> Foo<br>
<input type="checkbox" class="checkbox"> Bar<br>
<input type="checkbox" class="checkbox"> Hello<br>
<input type="checkbox" class="checkbox"> World<br>
<hr>
<input type="checkbox" class="hamlet"> to<br>
<input type="checkbox" class="hamlet"> be<br>
<input type="checkbox" class="hamlet"> or<br>
<input type="checkbox" class="hamlet"> not<br>
<input type="checkbox" class="hamlet"> to<br>
<input type="checkbox" class="hamlet"> be<br>
相关文章:
- 如何使用javascript检测数据表分页的其他页面中的复选框
- 在其他文本输入字段中输入文本时启用复选框
- 选中一个复选框后,使用html.checkbox自动禁用其他复选框
- 当选中一个复选框时,表单中的所有其他复选框都必须禁用-不工作
- 具有延迟的多个复选框选择以检查其他复选框更改
- Javascript错误,根据其他复选框的结果更新复选框/按钮
- 如果选中了其他复选框,则取消选中复选框
- 使用JavaScript获取widgetVar或选中/取消选中所有其他PrimeFaces复选框
- 仅从表中选中所有复选框,而不从其他表中选中所有复选框
- 复选框在选中时触发函数,如果未选中,则触发其他函数
- 选中后选中所有其他复选框
- 取消选中复选框将取消选中表中的其他复选框
- jQuery - 选中所有/取消选中所有复选框 - 然后取消选中(或选中)其他复选框
- 复选框状态和其他属性
- Jquery如果选中某个复选框,则禁用其他复选框
- 翡翠,如果其他速记选择复选框
- 复选框启用 Onload 以显示其他元素
- 单击具有类似类的复选框时,禁用其他复选框
- 如何在jsp中插入单选按钮、复选框和其他输入类型
- Javascript/JQuery设置复选框的值,同时禁用其他复选框