【发布时间】:2021-04-27 16:17:03
【问题描述】:
我想获取选中复选框的 id 并使用 jquery 将这些 id 存储在一个数组中。 谁能给我正确的代码。
我试过了
提前致谢:)
$("#merge_button").click(function(event){
event.preventDefault();
var searchIDs = $("#find-table input:checkbox:checked").map(function(){
return $(this).val();
}).toArray();
console.log(searchIDs);
});
【问题讨论】:
-
你可以把
return $(this).val();改成this.id -
我知道了,但它确实返回数组
-
它给出了逗号分隔的值。我无法循环遍历它
-
它给出了一个 id 数组,你可以根据需要循环遍历。
-
实际上查看源代码,
toArray()和get()现在几乎相同,并且都使用对slice()的调用,因此在现代版本的 jQuery 中没有区别。