how to pass the id of a row link to a jquery , in order to use ajax?
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." <a id='editcat' href=edit.php?id=".$key.">Edit</a> <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." <a id='editcat' href=edit.php?id=".$key.">Edit</a> <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." <a id='editcat_.$key' class="editcat" href=edit.php?id=".$key.">Edit</a> <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");
});
精彩评论