admin管理员组文章数量:1326099
How would you delete a row from a table using AJAX based on this code?
Here's the PHP code I'm working with:
foreach ($results as $result) {
echo "<tr><td>".$result['first_name']."</td><td>".$result['last_name']."</td><td><button class=\"btn btn-sm btn-danger delete_class\" id=\"".$result['id']."\" >DELETE</button></td></tr>";
}
As you can see, the button has an id paired with it.
Here's my jquery/AJAX code to delete the file from the database:
<script>
var $tr = $(this).attr('parentElement');
$('.delete_class').click(function(){
var del_id= $('.delete_class').attr('id');
$.ajax({
url:"delete_page.php?delete_id="+del_id,
cache:false,
success:function(result){
$tr.find('td').fadeOut(1000,function(){
$tr.remove();
});
}
});
});
</script>
There is also a PHP file that goes into the database and deletes the data, which works fine.
In the javascript code above ^ the variable being set at the top "$tr" is saying the 'parentAttribute' is the "td" not the "tr", how would I go up two parent attributes?
What can I change my "success:function(result){ }" to to make the row immediately disappear, because,
$tr.find('td').fadeOut(1000,function(){
$tr.remove();
}
this code ^ isn't working.
How would you delete a row from a table using AJAX based on this code?
Here's the PHP code I'm working with:
foreach ($results as $result) {
echo "<tr><td>".$result['first_name']."</td><td>".$result['last_name']."</td><td><button class=\"btn btn-sm btn-danger delete_class\" id=\"".$result['id']."\" >DELETE</button></td></tr>";
}
As you can see, the button has an id paired with it.
Here's my jquery/AJAX code to delete the file from the database:
<script>
var $tr = $(this).attr('parentElement');
$('.delete_class').click(function(){
var del_id= $('.delete_class').attr('id');
$.ajax({
url:"delete_page.php?delete_id="+del_id,
cache:false,
success:function(result){
$tr.find('td').fadeOut(1000,function(){
$tr.remove();
});
}
});
});
</script>
There is also a PHP file that goes into the database and deletes the data, which works fine.
In the javascript code above ^ the variable being set at the top "$tr" is saying the 'parentAttribute' is the "td" not the "tr", how would I go up two parent attributes?
What can I change my "success:function(result){ }" to to make the row immediately disappear, because,
$tr.find('td').fadeOut(1000,function(){
$tr.remove();
}
this code ^ isn't working.
Share Improve this question asked Jan 22, 2016 at 21:45 LatentDenisLatentDenis 2,99113 gold badges52 silver badges104 bronze badges3 Answers
Reset to default 3Change your current jquery
code as shown below:
<script>
$('.delete_class').click(function(){
var tr = $(this).closest('tr'),
del_id = $(this).attr('id');
$.ajax({
url: "delete_page.php?delete_id="+ del_id,
cache: false,
success:function(result){
tr.fadeOut(1000, function(){
$(this).remove();
});
}
});
});
</script>
The closest
method returns the first ancestor of the selected element.
https://api.jquery./closest/
HTML
<table>
<thead>
<tr>
<th>Page Name</th>
<th>Page Image</th>
<th>Action </th>
</tr>
</thead>
<tbody>
<tr>
<td>Page Name1</td>
<td>Page Image1</td>
<td><a title="Delete" class="dlt" href="delete.php?id=1" >Delete</a></td>
</tr>
<tr>
<td>Page Name1</td>
<td>Page Image1</td>
<td><a title="Delete" class="dlt" href="delete.php?id=2" >Delete</a></td>
</tr>
</tbody>
</table>
Javascript
<script>
$("a.dlt").click(function(evt){
evt.preventDefault();
if(confirm("It will Delete All detail related to this. Are You Sure? ")){
var dis = this;
$.post($(dis).attr('href'),{'delete':'dlt'},function(resp){
if(resp == 1){
$(dis).parent().parent().remove();
}else{
alert(resp);
}
});
}
});
</script>
Try
tr = $(this).parent();
there's no attribute called 'parentElement' that's why it's not working.
reference: Jquery Docs
I'd also change the function line to:
var del_id = $(this).attr('id');
本文标签: javascriptAJAX Delete row in table with PHP id selectedStack Overflow
版权声明:本文标题:javascript - AJAX Delete row in table with PHP id selected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742193461a2430642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论