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

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-06 20:44:16242browse

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!

Statement:
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