How to check/uncheck radio button on click?

后端 未结 18 1380
情深已故
情深已故 2020-11-29 02:16

I want to be able to uncheck a radio button by clicking on it.

So, if a radio button is unchecked, I want to check it, if it is checked, I want to uncheck it.

<
18条回答
  •  时光取名叫无心
    2020-11-29 02:43

    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.

提交回复
热议问题