SoFunction
Updated on 2025-02-28

Method to get the value of Radio element in jQuery

I also found that the JavaScript code I wrote ran error under FireFox. The reason was very depressed. When I got the value of a Radio element, I only got undefined.
Googled it, it was mostly the same method I used
Copy the codeThe code is as follows:

var value = $("input[name='radio1'][type='radio'][checked]").val();

This sentence passed the test under IE and Safari (3.2), but the selected value cannot be obtained under FireFox and Chrome.
After carefully looking at the manual, I found the list of "Form Object Properties". Is it said that there are special attribute judgment methods for form objects? Change the code
Copy the codeThe code is as follows:

var value = $("input[name='radio1'][type='radio']:checked").val();

The tests were passed under IE, FireFox, Chrome, and Safari (3.2).
By the way, I tested the select element. The writing method in the manual and the writing method I used frequently. You can get the correct value in the above browsers.
Copy the codeThe code is as follows:

var value1 = $("select").val(); var value2 = $("select option:selected").val();

I tested it under jQuery version 1.32, you can try it.