how to get multiple checkbox value using jquery

前端 未结 12 774
终归单人心
终归单人心 2020-11-29 20:11

How can I get the value of checkboxes which are selected using jquery? My html code is as follows:



        
12条回答
  •  不知归路
    2020-11-29 20:29

    You can do it like this,

    $('.ads_Checkbox:checked')
    

    You can iterate through them with each() and fill the array with checkedbox values.

    Live Demo

    To get the values of selected checkboxes in array

    var i = 0;
    $('#save_value').click(function () {
           var arr = [];
           $('.ads_Checkbox:checked').each(function () {
               arr[i++] = $(this).val();
           });      
    });
    

    Edit, using .map()

    You can also use jQuery.map with get() to get the array of selected checkboxe values.

    As a side note using this.value instead of $(this).val() would give better performance.

    Live Demo

    $('#save_value').click(function(){
        var arr = $('.ads_Checkbox:checked').map(function(){
            return this.value;
        }).get();
    }); 
    

提交回复
热议问题