开发者

Using jQuery to make checkboxes like radio how can I uncheck? fiddle included

I'm using this code to make checkboxes act like radio buttons. What can I add/change so when a checked box is clicked on it'll uncheck?

http://jsfiddle.net/peter/4Awf9/

$(".radiounique").click(function() {
$(".radiounique").removeAttr('checked');
$(this).attr('checked', true);
});

<input type="checkbox" class="radiounique" value="3">
<input type="checkbox" class="ra开发者_开发技巧diounique" value="4">
<input type="checkbox" class="radiounique" value="2">
<input type="checkbox" class="radiounique" value="1">


If you want it to act like the radiobuttons -

can try -

$(".radiounique").click(function() {
    var state = $(this).is(':checked');
    $(".radiounique").removeAttr('checked');
    $(this).attr('checked', state );
});

Fiddle - http://jsfiddle.net/4Awf9/1/


This is a common mistake. Radiobuttons are radiobuttons, checkboxes are checkboxes. Do not confuse your users by changing a checkbox list function to emulate a radiobutton list. That is very bad practice.

By the way, if you disable javascript it still will be possible to check multiple boxes.

I know this is not the answer you would like, but it's the truth.


    $(".radiounique").click(function() {

        var clicked = $(this);

        $(".radiounique").each(function(){
            if($(clicked[0]).attr('value') != $(this).attr('value'))
                 $(this).attr('checked', false);     
        })

    if($(this).attr('checked') == 'checked')
            $(this).attr('checked', true);

});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜