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

后端 未结 6 1802
再見小時候
再見小時候 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 13:50

    Try using the form of index that allows you to specify an element in a collection and returns it's relative position in the collection:

    var radioIdx = $(":radio[name='radioFieldName']")
                        .index($(":radio[name='radioFieldName']:checked")); 
    

    or the version that finds the first item matching a selector that is in another collection and reports it's position in the second collection.

    var radioIdx = $(":radio[name='radioFieldName']:checked")
                       .index(":radio[name='radioFieldName']");
    

提交回复
热议问题