How do I retrieve the names of all checked checkboxes using jQuery?
开发者_StackOverflow中文版I want to get the names of all checked checkboxes. I tried this:
$("#movebutton").click(function() {
var selectedids = "";
alert($('.checkbos_gruppe[checked=checked]').attr("name"));
});
Does anyone have a better idea?
You need to loop through them, in this case .map()
is a great choice to get an array of names, for example:
$("#movebutton").click(function() {
var selectednames = $('.checkbos_gruppe:checked').map(function() {
return this.name;
}).get();
alert(selectednames.join(", "));
});
This will get you an array of the name
attributes in selectednames
, then use it however you want. Make sure to use .get()
on the end so you get just an array of the names.
use:
var names = $('.checkbos_gruppe:checked').map(function(index, elem) {
return this.name;
}).get();
精彩评论