Get checkbox's ID

ⅰ亾dé卋堺 提交于 2019-12-19 10:01:44

问题


I need to get the ID of checkboxes in a table in the <tbody> section. I have a live(click) function that I want to bring up a popup with the data that i want based in the id in the checkbox.

I want to also allow the system, to have multiple checkboxes checked I want to be able to store the ids in a temp var and the use them as needed and get rid of them as I go though them.


回答1:


Just get id with .attr:

$("#container input:checkbox").attr("id")

To get an array of checkboxes ids you might use .map function:

var idArr = $("#container input:checkbox").map(function(i, el) { return $(el).attr("id"); }).get();



回答2:


$("#container input:checkbox") will be a jQuery object of all the checkboxes inside a particular div with an id of container. You can store that value and use it in the future to carry out all sorts of jQuery operations.

If you want to collect all the DOM objects into a separate array, you can just do a .get() on it.

var domObjects = $("#container input:checkbox").get();

If you want to get all the ids into an array, you could do it this way:

var idArray = [];
$("#container input:checkbox").each(function() {
    idArray.push(this.id);
});


来源:https://stackoverflow.com/questions/7384442/get-checkboxs-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!