jQuery check/uncheck radio button onclick

前端 未结 24 2663
滥情空心
滥情空心 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: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);
    });
    

提交回复
热议问题