How to check a radio button with jQuery?

前端 未结 30 2092
独厮守ぢ
独厮守ぢ 2020-11-22 08:12

I try to check a radio button with jQuery. Here\'s my code:

30条回答
  •  醉话见心
    2020-11-22 09:04

    In addition, you can check if the element is checked or not:

    if ($('.myCheckbox').attr('checked'))
    {
       //do others stuff
    }
    else
    {
       //do others stuff
    }
    

    You can checked for unchecked element:

    $('.myCheckbox').attr('checked',true) //Standards way
    

    You can also uncheck this way:

    $('.myCheckbox').removeAttr('checked')
    

    You can checked for radio button:

    For versions of jQuery equal or above (>=) 1.6, use:

    $("#radio_1").prop("checked", true);
    

    For versions prior to (<) 1.6, use:

    $("#radio_1").attr('checked', 'checked');
    

提交回复
热议问题