How to get id value for checkbox?

There is a table that dynamically generates text fields at runtime. I want to delete a row that has been checked. The table is associated with the database.

And the checkbox identifier was added by the TABLE DATA ID. If, check the box and click "Delete", it should be removed. I have end codes.

I just want to get the ID value of any checkbox that will be selected because it contains the row id along with its own id.

The dynamically generated window code is as follows.

x+= "<td>" + "<input id='cid"+id + "'type=checkbox />"

Any suggestions would be appreciated. Here I use JavaScript. JQuery notations are also useful to me. Thanks

Any conceptual representation of selecting more than 1 row and deleting will be more valuable.

Thanks again.

+3
source share
4 answers

EDIT: I am re-reading your question again ... again. I think I used to be wrong. It looks like the delete button is outside the table, and when you click it, you want to delete the rows in which the check box is selected.

Does it sound the way you want?

$('#deleteButton').click(function() {
      var $checked = $('#theTable :checkbox[id^=cid]:checked');
      $checked.closest('tr').each(function( i ) {
          var theID = $checked.eq( i ).attr('id'); // get the ID
          $(this).remove();
      });
});

You did not provide HTML, so I'm not sure exactly what the selector should look like.

+1
source

To get the checkbox id:

$('td input[type=checkbox]').click(function(){
    var id = $('td input').attr('id');
});

To select more than one row and then delete the values, you can do something like this:

$('form').submit(function(){
    $('td input:checked').each(function(){
        deleteFromDatabase($(this).attr('id'));
    });
});

, , . JS deleteFromDatabase Ajax, .

+1

I want to delete the line that got checked

// selector may need to be refined, depending on its precise location in the DOM
$(":checkbox").click(function() {
    if(this.checked) $(this).closest("tr").remove();
});
+1
source

To get the identifier of all checked flags, you can use this:

$('input[type=checkbox]:checked').attr('id');
+1
source

Source: https://habr.com/ru/post/1762002/


All Articles