开发者

how to pass the id of a row link to a jquery , in order to use ajax?

开发者 https://www.devze.com 2023-04-01 18:00 出处:网络
how to pass the id of an edit or delete link via jquery in order to use ajax and php ? here\'s my code at the front-end which displays the links

how to pass the id of an edit or delete link via jquery in order to use ajax and php ? here's my code at the front-end which displays the links

foreach($cat->getCategories() as $key => $val){
        echo $val."&nbsp;&nbsp;&nbsp;<a id='editcat' href=edit.php?id=".$key.">Edit</a>&nbsp;&nbsp;&nbsp;<a id='deletecat' href=delete.php?id=".$key.">Delete</a><br />";
}

now how to pass those id's to the开发者_C百科 jquery ?

   $('#editcat').click(function(){

   });

   $('#deletecat').click(function(){

   });


Modify your foreach like this:

foreach($cat->getCategories() as $key => $val){
        echo $val."&nbsp;&nbsp;&nbsp;<a id='editcat' href=edit.php?id=".$key.">Edit</a>&nbsp;&nbsp;&nbsp;<a id='deletecat' href=delete.php?id=".$key.">Delete</a><br />";
        echo "<input type="hidden" id="key" value=".$key." />";
}

Now in JS:

   $('#editcat').click(function(){
        var key = $('#key').val();
   });

   $('#deletecat').click(function(){
        var key = $('#key').val();
   });

Hope this helps.


Change your PHP, you must have unique ids on the markup.

foreach($cat->getCategories() as $key => $val){
        echo $val."&nbsp;&nbsp;&nbsp;<a id='editcat_.$key' class="editcat" href=edit.php?id=".$key.">Edit</a>&nbsp;&nbsp;&nbsp;<a id='deletecat_.$key' class='deletecat' href=delete.php?id=".$key.">Delete</a><br />";
}

jQuery code:

$('.editcat').click(function(){
    var id = (($(this).attr('id')).split('_', 2))[1];
    // do your other stuff here...
});

$('.deletecat').click(function(){
    var id = (($(this).attr('id')).split('_', 2))[1];
    // do your other stuff here...
});


Put the value of the $key into a rel tag, <a href='' rel='".$key."'>Edit</a>

And then use this in the function

$('#editcat').click(function(){    
   var id = $(this).attr("rel");    
});
0

精彩评论

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