Javascript 如何从php页面中删除整行
我从数据库中删除了该行,但我也想从php页面的界面中删除该行。任何帮助都将不胜感激 mypage.phpJavascript 如何从php页面中删除整行,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我从数据库中删除了该行,但我也想从php页面的界面中删除该行。任何帮助都将不胜感激 mypage.php <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['title']; ?></td> <td><div class="delete_class" id="<?php echo
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><div class="delete_class" id="<?php echo $row['id']; ?>">Delete</div></td>
</tr>
$(document).ready(function(){
$(".delete_class").click(function(){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
url:'check.php',
data:'delt_id='+del_id,
success:function(data) {
if(data) { alert("successfully deleted data");
} else { // DO SOMETHING }
}
});
});
});
check.php
$id = $_POST['delt_id'];
$query = "delete from TABLE NAME where ID = $id";
您需要删除
元素
// you currently have this
var del_id = $(this).attr('id');
// add this, step up to td, step up to tr
var $tableRow = $(this).parent().parent();
// or add this, finds the nearest tr
var $tableRow = $(this).closest('tr');
// then remove it (once you know the request was successful)
$tableRow.remove();
最简单的解决方案是在成功回调中删除它的客户端(如果只有在确定行已从datatabase服务器端删除时才触发成功):
$(文档).ready(函数(){
$(“.delete_class”)。单击(函数(){
var del_id=this.id;
$.ajax({
背景:这,//
$(document).ready(function () {
$(".delete_class").click(function () {
var del_id = this.id;
$.ajax({
context: this, // << set it for keeping context in jqXHR callbacks
type: 'POST',
url: 'check.php',
data: 'delt_id=' + del_id,
success: function (data) {
if (data) {
alert("successfully deleted data");
$(this).closest('tr').remove();
} else {
// DO SOMETHING ELSE
}
}
});
});
});