.attr('checked','checked') does not work

后端 未结 7 2216
醉话见心
醉话见心 2020-12-13 06:15

I am trying to check radio. Neither the following works:

[edit]

$(\'selector\').attr(\'checked\',\'checked\');
$(\'selector\').attr(\'checked\',true)         


        
7条回答
  •  情深已故
    2020-12-13 06:50

    It works, but

    $('input[name="myname"][checked]').val()
    

    will return the value of the first element with attribute checked. And the a radio button still has this attribute (and it comes before the b button). Selecting b does not remove the checked attribute from a.

    You can use jQuery's :checked:

    $('input[name="myname"]:checked').val()
    

    DEMO


    Further notes:

    • Using $('b').attr('checked',true); is enough.
    • As others mentioned, don't use inline event handlers, use jQuery to add the event handler.

提交回复
热议问题