Home > Web Front-end > JS Tutorial > How to Get the Value of a Selected Radio Button Using jQuery?

How to Get the Value of a Selected Radio Button Using jQuery?

Barbara Streisand
Release: 2024-12-07 01:14:10
Original
234 people have browsed it

How to Get the Value of a Selected Radio Button Using jQuery?

How to identify the selected option in a group of radio buttons with jQuery

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()
Copy after login

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());
});
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template