简体   繁体   中英

Get values of all checked radio buttons using JQuery

Using JQuery I am trying to perform validation and also get the values of all the dynamically generated Radio buttons in the page.

I have 10 questions on my page and each question has a Radio button group(YES/NO).

when click in the radio button i want to send it's value to database for the question, this is my code

<p>Question 1</p>
<input id="1_1" type="radio" name="1" value="Yes" />
<input id="1_2" type="radio" name="1" value="NO" />

i searched in google and found this code

$('input[name=radioName]:checked', '#myForm').val()

But I don't know what is the right way to use it ?

As per your HTML structure try this :-

var arr = []; // take an array to store values
$('input[type="radio"][name="1"]:checked').each(function(){
   arr.push($(this).val());  //push values in array
});

If you want the values of all <radio> you can do

var ans = $('[name=1]:checked').map(funciton(){
    return $(this).val();
}).get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM