jQuery check/uncheck radio button onclick

前端 未结 24 2408
滥情空心
滥情空心 2020-12-01 05:23

I have this code to check/uncheck a radio button onclick.

I know it is not good for the UI, but I need this.

$(\'#radioinstant\').click(function() {          


        
相关标签:
24条回答
  • 2020-12-01 05:42

    $(document).ready(function(){ $("input:radio:checked").data("chk",true);

    $("input:radio").click(function(){
        $("input[name='"+$(this).attr("name")+"']:radio").not(this).removeData("chk");
    
        $(this).data("chk",!$(this).data("chk"));
    
        $(this).prop("checked",$(this).data("chk"));
    });
    

    });

    0 讨论(0)
  • 2020-12-01 05:43

    If all you want to do is have a checkbox that checks, don't worry about doing it with JQuery. That is default functionality of a checkbox on click. However, if you want to do additional things, you can add them with JQuery. Prior to jQuery 1.9, you can use use $(this).attr('checked'); to get the value instead of $(this).attr('checked', true);, as the second will set the value.

    Here is a fiddle demonstration that shows the default checkbox functionality vs. what you are doing with JQuery.

    Note: After JQuery 1.6, you should use $(this).prop; instead of $(this).attr in all three places (thanks @Whatevo for pointing this out and see here for further details).

    UPDATE:

    Sorry, missed the requirement that it had to be a radio button. You still may want to consider the checkbox, but here is the updated code for the radio input version. It works by setting the previousValue as an attribute on the checkbox, as I don't think prop is supported in 1.3.2. You could also do this in a scoped variable, as some people don't like setting random attributes on fields. Here is the new example.

    UPDATE 2:

    As Josh pointed out, the previous solution only worked with one radio button. Here's a function that makes a group of radios deselectable by their name, and a fiddle:

    var makeRadiosDeselectableByName = function(name){
        $('input[name=' + name + ']').click(function() {
            if($(this).attr('previousValue') == 'true'){
                $(this).attr('checked', false)
            } else {
                $('input[name=' + name + ']').attr('previousValue', false);
            }
    
            $(this).attr('previousValue', $(this).attr('checked'));
        });
    };
    
    0 讨论(0)
  • 2020-12-01 05:43

    Best and shortest solution. It will work for any group of radios (with the same name).

    $(document).ready(function(){
        $("input:radio:checked").data("chk",true);
        $("input:radio").click(function(){
            $("input[name='"+$(this).attr("name")+"']:radio").not(this).removeData("chk");
            $(this).data("chk",!$(this).data("chk"));
            $(this).prop("checked",$(this).data("chk"));
            $(this).button('refresh'); // in case you change the radio elements dynamically
        });
    });
    

    More information, here.

    Enjoy.

    0 讨论(0)
  • 2020-12-01 05:44

    here is simple solution i hope it will help you. here's a simple example to improve answer.

    $('[type="radio"]').click(function () {
    
                if ($(this).attr('checked')) {
    
                    $(this).removeAttr('checked');
                    $(this).prop('checked',false);
    
                } else {
    
                    $(this).attr('checked', 'checked');
    
                }
            });
    

    Demo sorry for too late.. :)

    0 讨论(0)
  • 2020-12-01 05:44

    The solutions I tried from this question did not work for me. Of the answers that worked partially, I still found that I had to click the previously unchecked radio button twice just to get it checked again. I'm currently using jQuery 3+.

    After messing around with trying to get this to work using data attributes or other attributes, I finally figured out the solution by using a class instead.

    For your checked radio input, give it the class checked e.g.:

    <input type="radio" name="likes_cats" value="Meow" class="checked" checked>
    

    Here is the jQuery:

    $('input[type="radio"]').click(function() {
        var name = $(this).attr('name');
    
        if ($(this).hasClass('checked')) {
            $(this).prop('checked', false);
            $(this).removeClass('checked');
        }
        else {
            $('input[name="'+name+'"]').removeClass('checked');
            $(this).addClass('checked');
        }
    });
    
    0 讨论(0)
  • 2020-12-01 05:45

    Simplest solution. For both Radio and Checkboxes.

    $('body').on('click', 'input[type="checkbox"]', function(){
        if ($(this).attr('checked')){
            $( this ).attr( 'checked', false);
        } else {
            $( this ).attr( 'checked', true);
        }
    });
    $('body').on('click', 'input[type="radio"]', function(){
        var name = $(this).attr('name');
        $("input[name="+name+"]:radio").attr('checked', false);
        $( this ).attr( 'checked', true);
    });
    
    0 讨论(0)
提交回复
热议问题