开发者

Checkbox behaviour modification with javascript

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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜