简体   繁体   English

获取所有检查值的列表

[英]Get a list of all checked values

I have a bunch of radio boxes on my HTML page. 我的HTML页面上有一堆单选框。 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"/>

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? 使用Javascript或Jquery,如何创建一个包含所有复选框值的数组对象?

I have an array ( questionsArray ) that holds the name values for the radio box sets. 我有一个数组( questionsArray ),它保存单选框集的name值。

I tried doing this: 我尝试这样做:

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

Not only does this throw an exception cannot access value of undefined , it is not adding them to an array either. 这不仅引发异常cannot access value of undefined ,也没有将它们添加到数组中。

Any help would be much appreciated. 任何帮助将非常感激。

The following will put the value of the value attribute for each selected radio button into the arr Array: 下面将把每个选中的单选按钮的value属性value放入arr Array:

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

Try this code 试试这个代码

$('#buttonId').click( function() {
    var result = '';
    $('input:checked').each(function() {
        result += ',' + $(this).val();
    });
    alert(result);
});
$( '#my-button-id' ).click( function() {
    var tmp = '';
    $( '.custom:checked' ).each( function() {
        // Do what you want...
        tmp += ' ' + $( this ).val();
    });
    alert( tmp );
});

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

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