【发布时间】:2015-06-06 01:14:20
【问题描述】:
我需要在 html 中只选择一组中的一个复选框。 我找到了我的例子。但我无法意识到。它不起作用。 这是我的例子:example
这是我的代码 HTML:
<div>
<li>
<div class="radio">
<label>
<input type="checkbox" name="mobil[1][]" id="optionsRadios1" value="1" checked class="radi">
Сотовый телефон имеется
</label>
</div>
</li>
<li>
<div class="radio">
<label>
<input type="checkbox" name="mobil[1][]" id="optionsRadios2" value="0" class="radi">
Сотовый телефон не имеется
</label>
</div>
</li>
<div>
和代码jquery:
$("input:checkbox").on('click', function() {
// in the handler, 'this' refers to the box clicked on
var $box = $(this);
if ($box.is(":checked")) {
// the name of the box is retrieved using the .attr() method
// as it is assumed and expected to be immutable
var group = "input:checkbox[name='" + $box.attr("name") + "']";
// the checked state of the group/box on the other hand will change
// and the current value is retrieved using .prop() method
$(group).prop("checked", false);
$box.prop("checked", true);
}
else {
$box.prop("checked", false);
}
});
任何帮助请。
【问题讨论】:
-
使用单选按钮,而不是复选框!
-
@gvee 我需要使用复选框。
-
注意
li应该是ul的子元素或ol元素! -
@Vohuman 感谢您的关注。
-
是允许选中还是取消选中 0 或 1 个复选框?还是在检查单个值后禁用所有其他值?