Home >Web Front-end >JS Tutorial >How Can I Get the Value of a Selected Radio Button Using jQuery?
jQuery: Determining Selected Radio Button
When working with radio button groups in HTML forms, it's often necessary to obtain the value of the selected option using JavaScript for processing and form submission. jQuery provides a convenient method to accomplish this task.
To get the value of the selected radio button with a given name (radioName), use this syntax:
$('input[name=radioName]:checked', '#myForm').val()
Here's an example:
<form>
$('#myForm input').on('change', function() { alert($('input[name=radioName]:checked', '#myForm').val()); });
This code uses jQuery to attach an event listener to the radio buttons within the form. When any radio button is clicked, the selected value is obtained and displayed in an alert dialog. This approach can be used to perform custom operations based on the selected radio button.
The above is the detailed content of How Can I Get the Value of a Selected Radio Button Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!