简体   繁体   English

使用JQuery获取所有选中的单选按钮的值

[英]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. 使用JQuery我正在尝试执行验证,并获取页面中所有动态生成的单选按钮的值。

I have 10 questions on my page and each question has a Radio button group(YES/NO). 我的页面上有10个问题,每个问题都有一个单选按钮组(是/否)。

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 :- 根据您的HTML结构,试试这个: -

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 如果你想要所有<radio>的值,你可以做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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