我需要在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);
}
});
任何幫助PLZ。
使用單選按鈕而不是複選框! – gvee 2015-04-01 11:48:36
@gvee我需要使用複選框。 – 2015-04-01 11:49:21
請注意'li'應該是'ul'或'ol'元素的孩子! – undefined 2015-04-01 11:50:19