开发者

How to pass multiple selected (through checkboxes) row ids to a php script in jQuery?

开发者 https://www.devze.com 2022-12-19 17:26 出处:网络
I\'m generating an html file which looks like: <tr id=\"ID001\" property1=\"PROPERTY001\"><td><input type=\"checkbox\"

I'm generating an html file which looks like:

<tr id="ID001" property1="PROPERTY001"><td><input type="checkbox" 
       name="row_checkbox_ID001"></td><td>...</td><td>...</td></tr>
<tr id="ID002" property1="PROPERTY002"><td><input type="checkbox" 
       name="row_checkbox_ID002"></td><td>...</td><td>...</td></tr>

When the user selects individual rows for deletion, how can I (through jQuery), pass this to a php script?

I will need to build something like this:

$(document).ready(function () {
    $('#contact-form input.contact-delete').click(function (e) {
        e.preventDefault();
        $.get("deleterecord.php", ...);
    });
});

There can be 0, 1 or multiple rows... The HTML being generated is under my control and can be modified.


Clarification:

I want to have a button above all these rows which the user can click on开发者_Python百科 AFTER he has selected the rows from the table.

<div id='contact-form'><h2>Contacts</h2>
<input type='button' name='contact-delete' value='Delete Record(s)' 
class='contact-delete'/>

The TRs need to be deleted, but BEFORE that, the deleterecord.php script needs to be called with the TR ids.


Use HTML arrays

<input type="checkbox" name="chk[]" value="01234">
<input type="checkbox" name="chk[]" value="98765">


You could create a JSON object containing details of any rows selected (ID's or whatever you need) and send that to your php script. Check out this article on json, php + jquery.


I believe, you are looking for similar behavior:

$('#contact-form input.contact-delete').click(function (e) {
    var $this = $(this);
    // find the table row, in which are elements contained
    var $tr = $this.closest('tr');
    // take id
    var id = $tr.attr('id');
    // ajax with id
    $.get("deleterecord.php?id="+id, function (data) {
        // remove table row on success
        $tr.remove();
    });
    e.preventDefault();
});


Sorry for answering my own question, but the SO post at: Getting all selected checkboxes in an array neatly solves my problem!

For future visitors to this page, pay attention to variable names in your HTML and how you're accessing the input checkboxes in jQuery!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号