简体   繁体   中英

how to get the values of checkboxes that are checked using jquery

I have a list of dynamically populated check boxes with values. This code works but with some unnecessary stuffs like text,free text,free text,etc with the value of my checked checkbox

JS:

$('#save').on('click', function () {
    var val = $(':checked').map(function () {
        return this.value;
    }).get()

    alert(val);
});

Limit your selector to only checbox elements

//or you can use $(':checkbox:checked')
var val = $('input[type="checkbox"]:checked').map(function () {
    return this.value;
}).get()

Demo: Fiddle

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