When working with forms, it's often necessary to retrieve the value of selected radio buttons. jQuery provides a straightforward way to accomplish this.
Consider the following scenario: you have a form with multiple radio buttons named radioName. You wish to post the value of the selected option. To retrieve the value with jQuery, you can leverage the following code:
$('input[name=radioName]:checked', '#myForm').val()
This code searches for all input elements with name attribute set to radioName within the form with id myForm. The :checked selector ensures that only the selected radio button is selected. Finally, the .val() method returns the value of the selected radio button.
For example, the following code snippet demonstrates how to capture the value of the selected radio button upon selection change:
$('#myForm input').on('change', function() { alert($('input[name=radioName]:checked', '#myForm').val()); });
The above is the detailed content of How to Get the Value of a Selected Radio Button Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!