Abrufen von Kontrollkästchenlistenwerten mit jQuery
Lesezeit: 3 Minuten
Kris-I
In einem div habe ich ein Kontrollkästchen. Ich möchte, wenn ich eine Taste drücke, dass alle Namen aller Kontrollkästchen aktiviert werden. Können Sie mir sagen, wie das geht?
$(document).ready(function() {
$('#someButton').click(function() {
var names = [];
$('#MyDiv input:checked').each(function() {
names.push(this.name);
});
// now names contains all of the names of checked checkboxes
// do something with it
});
});
Warum ist es so einfach, wenn du mir die Lösung gibst?! 😉
– Kris-I
17. August 2009 um 12:20 Uhr
Ich konnte dieses Array nicht in Java abrufen, ich habe String verwendet[] aber dennoch. Der Fehler tritt auf, wenn die Variable über die .post-Methode übergeben wird. com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn Fehler beim Festlegen des Ausdrucks „names[]’mit Wert'[Ljava.lang.String;@4a004a0′
– changeme
Jun 14, 2012 at 21:36
You can also use .map().
– Josh Crozier
Jan 15, 2015 at 3:41
Since nobody has mentioned this..
If all you want is an array of values, an easier alternative would be to use the .map() method. Just remember to call .get() to convert the jQuery object to an array:
var elements = document.querySelectorAll('.parent input:checked');
var names = Array.prototype.map.call(elements, function(el, i) {
return el.name;
});
console.log(names);
var elements = document.querySelectorAll('.parent input:checked');
var names = Array.prototype.map.call(elements, function(el, i){
return el.name;
});
console.log(names);
Oder ‘boxen‘ würde auch akzeptiert werden 😛
– Molombie
23. August 2012 um 4:08 Uhr