jQuery: how to get the index of a checked radio button

后端 未结 6 1889
再見小時候
再見小時候 2021-02-12 13:03

I recently came across a StackOverflow answer that gave excellent instructions on how to get the value of a checked radio button using jQuery:

var radioVal = $(\         


        
6条回答
  •  没有蜡笔的小新
    2021-02-12 14:02

    There are a couple of options:

    1) Enumerate through the radio button list (aka without the :checked modifier) and test to see if it is checked; if so, you have the id of the element in the group.

    2) The better way (imo), is to simply associate some data with each element and pull that data. So if you give the element a 'data-index' attribute with the value, you can then simply call

    $('#myFormID input:radio[name='radioFieldName']:checked').data('index')
    

    And have the value.

提交回复
热议问题