简体   繁体   中英

How to get an array of class-names from a set of checked checkboxes using jQuery or JavaScript?

I'm using the following to get an array of values from a set of checkboxes that have been checked by the end-user:

var array = jQuery('input:checkbox[name=items]:checked').map(function() {
    return this.value;
}).get();

Instead of an array of values, I'd like to get an array of classnames .

My checkboxes:

<input type="checkbox" name="items" value="vOne" class="cOne">
<input type="checkbox" name="items" value="vTwo" class="cTwo">
<input type="checkbox" name="items" value="vThree" class="cThree">

I've tried the following which doesn't work:

var array = jQuery('input:checkbox[name=items]:checked').map(function() {
    return this.class;
}).get();

代替this.class ,您应该使用:

this.className

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM