I have got this group of checkboxes that behave like radio buttons.
I need some help in letting the user allow to uncheck a checkbox too, so there can be no selection within the group.
<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" clas开发者_运维技巧s="chb" /> CheckBox4</label>
$(".chb").each(function()
{
$(this).change(function()
{
$(".chb").attr('checked',false);
$(this).attr('checked',true);
});
});
$(".chb").change(function()
{
$(".chb").parent().siblings()
.find('input').prop('checked', false);
});
Or even better:
var $inputs = $(".chb");
$inputs.change(function()
{
$inputs.not(this).prop('checked', false);
});
And here's the fiddle: http://jsfiddle.net/zjJKc/
精彩评论