简体   繁体   中英

Get all checked checkboxes in certain div by click

I have a custom asp.net control which contains some checkBoxes. I know how to get the checkBox, that was clicked

$('#customer-category-control input:checkbox').click(function(event)
 {   
   var el =  $(this).attr('name');
 };

Suggest me, please, how to get only all checked checkBoxes by click and make a JSON object from their names.

var names=[];
$('#customer-category-control input[type="checkbox"]:checked').each(function (i, el){
    names.push(el.name);
    });
console.log(names); // => ['foo','bar'...]

Try this:

var obj = [];
$('#customer-category-control input[type=checkbox]:checked').each(function(index, value) {
    obj.push($(this).attr("name"));
});
    $(document).ready(function ()  
        {
           $('#customer-category-control input:checkbox').click(function(event)
               {
               var obj = [];
               $('#customer-category-control input[type=checkbox]:checked').each(function(index, value)
                   {  obj.push($(this).attr("name"));      });
alert(obj);

           });
        });

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