遍历列表并在 jquery 中检查复选框 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18955340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate through the list and get checked checkbox id in jquery
提问by Sanjay Malhotra
I have got id's of all the row which have check box in a variable.
我已经获得了所有在变量中带有复选框的行的 ID。
var allIds = jQuery("#progAccessSearchResults").jqGrid("getDataIDs");
Now I have to iterate over this and get id's of only checked check boxes. I tried following code to get checked checkbox id.
现在我必须迭代这个并获取仅选中复选框的 ID。我尝试使用以下代码来检查复选框 ID。
var boxes = $(":checkbox:checked");
But it is not working. Help me out..!! I am new to javascript n jquery. So pls don't mind if it is a silly problem..!!
但它不起作用。帮帮我..!!我是 javascript n jquery 的新手。所以请不要介意这是否是一个愚蠢的问题..!!
回答by CodingIntrigue
You can use .mapto find all IDs of checked checkboxes, using your existing selector:
您可以使用.map使用现有选择器查找选中复选框的所有 ID:
HTML:
HTML:
<input type="checkbox" id="cb1" />
<input type="checkbox" id="cb2" />
<input type="checkbox" id="cb3" checked="checked" />
<input type="checkbox" id="cb4" />
<input type="checkbox" id="cb5" checked="checked" />
Javascript:
Javascript:
var checkedIds = $(":checkbox:checked").map(function() {
return this.id;
}).get();
Returns: [ "cb3", "cb5" ]
返回: [ "cb3", "cb5" ]
Hereis a fiddle for the above code.
这是上述代码的小提琴。
回答by NaYaN
try with loop each
of jquery
尝试用循环each
的jquery
$("input:checkbox").each(function(){
var $this = $(this);
if($this.is(":checked")){
console.log($this.attr("id"));
}
});
回答by iJade
回答by Oleg
You can use first :nth-childselector to get <td>
elements having checkboxs in the grid. Then you can use >input:checked
selector to get all checked checkboxs. After you have jQuery object which contains the requested checkboxes you can use .closest("tr.jqgrow")
to get rows having the checkboxes. The ids of the rows is what you need. You can use $.map
to get all ids in the array. The full code will be
您可以使用 first :nth-child选择器来获取<td>
网格中具有复选框的元素。然后您可以使用>input:checked
选择器来获取所有选中的复选框。在您拥有包含请求的复选框的 jQuery 对象后,您可以使用它.closest("tr.jqgrow")
来获取具有复选框的行。行的 id 是您所需要的。您可以使用$.map
来获取数组中的所有 id。完整的代码将是
$("#getIds").button().click(function () {
var $checked = $grid.find(">tbody>tr.jqgrow>td:nth-child(" +
(iCol + 1) + ")>input:checked"),
ids = $.map($checked.closest("tr.jqgrow"),
function (item) { return item.id; });
alert("The list of rowids of the rows with checked chechboxs:\n" + ids.join());
});
where the index of the column with checkboxs iCol
you can get using getColumnIndexByName
function
iCol
您可以使用getColumnIndexByName
函数获得带有复选框的列的索引
var getColumnIndexByName = function (grid, columnName) {
var cm = grid.jqGrid("getGridParam", 'colModel'), i, l;
for (i = 0, l = cm.length; i < l; i += 1) {
if (cm[i].name === columnName) {
return i; // return the index
}
}
return -1;
};
which I frequently used in my old answers.
我经常在我的旧答案中使用它。
The demodemonstrate the above code live.
该演示现场演示了上述代码。