简体   繁体   中英

jQuery table - How to write text in cell depending on checkbox checked

I have a simple table with class=".table" contain 4 columns with classes as below

•.delete //this is checkbox column ,

•.Id,

•.description,

•.flag

and have a button with class=".savebtn" . What I need: when user check any checkbox cell in ".delete" column update flag cell in the same row with text "d" then delete this checked row when user click ".savebtn" .

I Try this code but not working please help me:

 $('.savebtn').click(function() {
  $('.cf-table-block tbody tr').each(function() {
    $(this).find($("input[name='delete[]']:checked")).each(function() {
      $(this).change(function() {
        $(this).closest("tr").find(".flag input").val("d"));  
        $(this).closest('tr').remove();
       });
   });
  });
 });

the element attribute should be class="table" like this

HTML

<p class="myClass"></p>

JQuery

$(".myClass").click( ...

EDIT

you can use a simpler function:

$('.savebtn').click(function() {
  $("td input.delete:checked").each(function(index, element) {
    $(element).closest("tr").remove();
  });
});

what it does is when the button with the class "savebtn" is clicked, it finds all input elements with a class "delete" that are checked. For each of them it finds the closest tr and deletes it.

 $('.savebtn').click(function() { $('.cf-table-block tbody tr').each(function() { console.log($(this)) $(this).find($(".delete:checked")).each(function() { $(this).closest("tr").find("input:text.flag").val("d"); //$(this).closest('tr').remove(); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border="1" class='cf-table-block'> <tr> <th>Delete</th> <th>ID</th> <th>Description</th> <th>Flag</th> </tr> <tr> <td align="center"> <input type="checkbox" class="delete"> </td> <td>any Data</td> <td>any Data</td> <td> <input type="text" class="flag" /> </td> </tr> <tr> <td align="center"> <input type="checkbox" class="delete"> </td> <td>any Data</td> <td>any Data</td> <td> <input type="text" class="flag" /> </td> </tr> <tr> <td align="center"> <input type="checkbox" class="delete"> </td> <td>any Data</td> <td>any Data</td> <td> <input type="text" class="flag" /> </td> </tr> <input class="savebtn" style="width: 65px; font-size: 16px;" type="button" value="Save"> 

$('.savebtn').click(function() {
  $('.cf-table-block tbody tr').each(function() {//added the class in html
    console.log($(this))
    $(this).find($(".delete:checked")).each(function() {//no attr name but class
      $(this).closest("tr").find("input:text.flag").val("d");//input type text with class flag
      //$(this).closest('tr').remove();//commented to see the letter d
    });
  });

});

Do this

DEMO

use this:

$('.savebtn').click(function() {
  $('.cf-table-block tbody tr').each(function() {
    $(this).find($("input[name='delete[]']:checked")).each(function() {
        if($(this).closest("tr").find(".flag input").text().indexOf("d") > -1)){ 
        $(this).closest('tr').remove();
         }
      });
  });
 });

You can do it like following.

 $('.savebtn').click(function() { $('.delete :checkbox:checked').closest('tr').remove(); }); $('.delete :checkbox').change(function() { var d = this.checked ? 'd' : ''; $(this).closest("tr").find(".flag").val(d); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border="1"> <tr> <th>Delete</th> <th>ID</th> <th>Description</th> <th>Flag</th> </tr> <tr> <td class="delete"><input type="checkbox"/></td> <td>any data</td> <td>any data</td> <td><input type="text" class="flag" /></td> </tr> <tr> <td class="delete"><input type="checkbox"/></td> <td>any data</td> <td>any data</td> <td><input type="text" class="flag"/></td> </tr> <tr> <td class="delete"><input type="checkbox" /></td> <td>any data</td> <td>any data</td> <td><input type="text" class="flag" /></td> </tr> </table> <input type="button" class="savebtn" value="Save"/> 

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