循环浏览页面上所有选中的复选框并将id拉入数组不起作用
Looping through all checked checkboxes on page and pulling id into array not working
更新
我现在已经解决了这个问题,更改了这一行:
idList.push(this.id);
至:
$(this).attr('id')
我试图循环浏览页面上的所有复选框,并将所有被选中的ID添加到数组中。我让它在每个复选框中循环,并检测它是否被选中,但我在将每个被选中复选框的ID添加到数组中时遇到了问题。
有人能告诉我哪里出了问题吗?
谢谢。
Javascript:
if (element == 'deleteButton' || element == 'allowButton')
{
//For testing.
//alert("Button Clicked");
//Create an empty array, so it can be re-sized at run time.
var idList = [];
//Loop through all checkboxes, adding each ones id to an array.
$('#reported_rages').find('input[type="checkbox"]:checked').each(function () {
//this is the current checkbox
//For testing.
//alert("Checked");
//Add the current ID to the array
idList.push(this.id);
});
//return false; // or do something else.
}
alert(idList);
这里尝试实现这些想法。
JSFiddle
/*sets variable to global so it can be accessed outside of function*/
var idList = [];
function run(){
/*clears the original variable*/
idList = [];
$('#data').find('input[type="checkbox"]:checked').each(function () {
idList.push(this.id);
});
/*prints after list is populated*/
console.log(idList);
$('#results').html(idList);
}
/*binds run function to click of submit*/
$('#submit').click(run);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='data'>
<input id='1' type='checkbox' />
<input id='2' type='checkbox' />
<input id='3' type='checkbox' />
<input id='4' type='checkbox' />
<input id='5' type='checkbox' />
</div>
<div id='results'></div>
<button id='submit'>Submit</button>
相关文章:
- Javascript复选框函数:;缺少:在属性id之后"
- 点击后,比较id'所选复选框的s以列出href
- 如何获取字段集元素中复选框的所有ID
- 使用jQuery获取所有未选中复选框的ID
- Bootstrap使用复选框显示和隐藏id
- 获取不带Id JQuery的复选框值
- 使用 Jquery 选中基于 Div ID 和类的所有复选框
- 将所有选中的复选框 ID 放入一个字符串中
- 更改转发器中的复选框 ID
- 遍历列表并在jquery中选中复选框id
- 使用 JavaScript 将选定的复选框 ID 传递到弹出窗口
- 根据复选框id值填充输入字段
- 改变复选框.Id通过javascript
- jqGrid设置多选复选框id和名称为不同的值
- 如何将gridview中的复选框id从vb.net传递到javascript
- 无法获取.net c#中jquery的asp:复选框ID
- 如何循环访问复选框 ID 并将其传递到 SQL DB
- 需要一个在JSP上读取动态复选框ID的JavaScript函数
- 在Javascript中添加复选框ID到数组
- Jquery,如果复选框被选中,则使用具有该复选框id的变量进行检查