我有这组复选框,其行为类似于单选按钮。
我需要一些帮助,让用户也可以取消选中复选框,因此组内无法选择。
<label><input type="checkbox" name="cb1" class="chb" /> CheckBox1</label>
<label><input type="checkbox" name="cb2" class="chb" /> CheckBox2</label>
<label><input type="checkbox" name="cb3" class="chb" /> CheckBox3</label>
<label><input type="checkbox" name="cb4" class="chb" /> CheckBox4</label>
$(".chb").each(function()
{
$(this).change(function()
{
$(".chb").attr('checked',false);
$(this).attr('checked',true);
});
});
答案 0 :(得分:3)
$(".chb").change(function()
{
$(".chb").parent().siblings()
.find('input').prop('checked', false);
});
甚至更好:
var $inputs = $(".chb");
$inputs.change(function()
{
$inputs.not(this).prop('checked', false);
});