简体   繁体   中英

How to know which tables's checkbox is selected?

This is a function

function collect_users_and_groups() {
   var tos = [];
   $('#mytable12 input:checked, #groupsTable1 input:checked').each(function(i, elt) {
       //alert("to groups");
       var dataids = $(this).parent().attr("data-selected").split(",");
       alert("dataids  "+dataids);
       var name = $.trim($(this).parent().next().text());
       tos.push(name);
   });

   return tos.join(', ');
}

which is called when I select check boxes Actually groupsTable1 has data-selected attribute but mytable12 does not have. I want to call this var dataids = $(this).parent().attr("data-selected").split(",");

when checkbox of groupsTable1 is clicked Please tell me how to do?

This is the full fiddle ,the above js codes can be found in between 22-33 in the js section

You can use .is() to check table is groupsTable1.

function collect_users_and_groups() {
   var tos = [];
   $('#mytable12 input:checked, #groupsTable1 input:checked').each(function(i, elt) {

        //Check table is groupsTable1
        if($(this).closest('table').is("#groupsTable1")){
            //alert("to groups");
            var dataids = $(this).parent().attr("data-selected").split(",");
            alert("dataids  "+dataids);
            var name = $.trim($(this).parent().next().text());
            tos.push(name);
        }
   });
   return tos.join(', ');
}

OR

Simply use

  $('#mytable12 input:checked, #groupsTable1 input:checked').each(function(i, elt) { 

instead of

  $('#groupsTable1 input:checked').each(function(i, elt) { 

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