获取所有检查值的列表

获取所有检查值的列表

本文介绍了获取所有检查值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML页面上有一堆单选框.像这样:

I have a bunch of radio boxes on my HTML page. Somewhat like this:

<input type="radio" name="19" class="custom" value="1"/>
<input type="radio" name="19" class="custom" value="2"/>
<input type="radio" name="19" class="custom" value="3"/>

<input type="radio" name="20" class="custom" value="4"/>
<input type="radio" name="20" class="custom" value="5"/>
<input type="radio" name="20" class="custom" value="6"/>

<input type="radio" name="21" class="custom" value="7"/>
<input type="radio" name="21" class="custom" value="8"/>
<input type="radio" name="21" class="custom" value="9"/>

选择所有单选框后,用户单击一个按钮.使用Javascript或Jquery,如何创建一个包含所有复选框值的数组对象?

After selecting all radio boxes, the user hits a button. Using Javascript or Jquery, how can I create an array object that holds the values of all checked boxes?

我有一个数组(questionsArray),该数组保存单选框集的name值.

I have an array (questionsArray) that holds the name values for the radio box sets.

我尝试这样做:

for (var i=0; i<questionsArray.length; i++)
{
    document.write(document.getElementsByName(questionsArray[i])[0].value + "<br/>");
}

这不仅会引发异常cannot access value of undefined,也不会将它们添加到数组中.

Not only does this throw an exception cannot access value of undefined, it is not adding them to an array either.

任何帮助将不胜感激.

推荐答案

下面将把每个选定单选按钮的value属性值放入arr数组:

The following will put the value of the value attribute for each selected radio button into the arr Array:

var arr = [];
$(".custom:checked").each(function () {
    arr.push($(this).val());
});

这篇关于获取所有检查值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:51