jQuery check/uncheck radio button onclick

前端 未结 24 2409
滥情空心
滥情空心 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:54

    If you use on click and only check if radio is checked and then deselect, you will never get the radio checked. So maybe easiest is to use classnames like this:

    if($(this).hasClass('alreadyChecked')) {//it is already checked
            $('.myRadios').removeClass('alreadyChecked');//remove from all radios
            $(this).prop('checked', false);//deselect this
        }
        else {
            $('.myRadios').removeClass('alreadyChecked');//remove from all
            $(this).addClass('alreadyChecked');//add to only this
        }
    
    0 讨论(0)
  • 2020-12-01 05:54

    I think this is the shortest way. I tested it on Chrome and MS Edge.

    $(document).on('click', 'input:radio', function () {
        var check = $(this).attr('checked')
        if (check) $(this).removeAttr('checked').prop('checked',false)
        else $(this).attr('checked', true).prop('checked',true)
    })
    

    This piece of code also works on AJAX loaded contents.

    Alternatively, You can also use

    $(document).on('click mousedown', 'input:radio', function (e) {
        e.preventDefault()
        if ($(this).prop('checked')) $(this).prop('checked', false)
        else $(this).prop('checked', true)
    })
    

    This would work better without any exceptions.

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

    I would suggest this:

    $('input[type="radio"].toggle').click(function () {
        var $rb = $(this);
    
        if ($rb.val() === 'on') {
            $rb.val('off');
            this.checked = false;
        }
        else {
            $rb.val('on');
            this.checked = true;
        }
    });
    

    Your HTML would look like this:

    <input type="radio" class="toggle" value="off" />
    
    0 讨论(0)
  • 2020-12-01 05:57

    toggleAttr() is provided by this very nice and tiny plugin.

    Sample code

    $('#my_radio').click(function() {
        $(this).toggleAttr('checked');
    });
    
    /**
     * toggleAttr Plugin
     */
    jQuery.fn.toggleAttr = function(attr) {
        return this.each(function() {
            var $this = $(this);
            $this.attr(attr) ? $this.removeAttr(attr) : $this.attr(attr, attr);
        });
    };
    

    Even more fun, demo

    You can use place your radio button inside label or button tags and do some nice things.

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

    I took https://stackoverflow.com/a/13575528/80353 and adapted it like this

    $(document).ready(function() {
    
            $('body').on('click', 'input[type="radio"]', function() {
                changeCheckedAttributes($(this));
            });
            function changeCheckedAttributes(elt) {
                $("input[name='"+$(elt).attr("name")+"']:radio").not($(elt)).removeData("chk");
                $("input[name='"+$(elt).attr("name")+"']:radio").not($(elt)).removeAttr("checked");
                $("input[name='"+$(elt).attr("name")+"']:radio").not($(elt)).prop("checked", false);
                $(elt).data("chk",!$(elt).data("chk"));
                $(elt).prop("checked", true);
                $(elt).attr("checked", $(elt).data("chk"));
            }
    
        });
    
    0 讨论(0)
  • 2020-12-01 06:01

    This last solution is the one that worked for me. I had problem with Undefined and object object or always returning false then always returning true but this solution that works when checking and un-checking.

    This code shows fields when clicked and hides fields when un-checked :

    $("#new_blah").click(function(){
       if ($(this).attr('checked')) {
    
                $(this).removeAttr('checked');
        var radioValue = $(this).prop('checked',false);
        // alert("Your are a rb inside 1- " + radioValue);
        // hide the fields is the  radio button is no     
        $("#new_blah1").closest("tr").hide();
        $("#new_blah2").closest("tr").hide();
    
        } 
        else {
    
        var radioValue =  $(this).attr('checked', 'checked');
        // alert("Your are a rb inside 2 - " + radioValue);
        // show the fields   when radio button is set to  yes
        $("#new_blah1").closest("tr").show();
        $("#new_blah2").closest("tr").show();
    
    }
    
    0 讨论(0)
提交回复
热议问题