Set selected radio from radio group with a value

后端 未结 10 587
深忆病人
深忆病人 2020-12-04 06:04

Why am I struggling with this?

I have a value: 5

How do I check the radio button of group \"mygroup\" with the value of 5?

$(\"input[name=myg         


        
10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 06:48

    There is a better way of checking radios and checkbox; you have to pass an array of values to the val method instead of a raw value

    Note: If you simply pass the value by itself (without being inside an array), that will result in all values of "mygroup" being set to the value.

    $("input[name=mygroup]").val([5]);
    

    Here is the jQuery doc that explains how it works: http://api.jquery.com/val/#val-value

    And .val([...]) also works with form elements like , , and s inside of a

    提交评论

提交回复
热议问题