我正在尝试简单删除ajax函数。我根本不是很好的广告JS,但当我点击按钮删除时,我可以在Chrome开发者控制台中看到id = NULL,当我刷新页面时,条目又回来了。 ajax函数非常简单
$("body").on("click",".remove-item",function(){
var entry_id = $(this).parent("td").data('entry_id');
var c_obj = $(this).parents("tr");
$.ajax({
dataType: 'json',
type:'POST',
url: 'delete.php',
data:{entry_id:entry_id}
}).done(function(data){
c_obj.remove();
toastr.success('Item Deleted Successfully.', 'Success Alert', {timeOut: 5000});
});
});
按钮
<button class="btn btn-danger remove-item" > Delete</button>
这就是delete.php
$entry_id = $_POST["entry_id"];
$stmt = $pdo->prepare("DELETE FROM entries WHERE entry_id = :entry_id");
$stmt->bindParam(':entry_id', $entry_id, PDO::PARAM_INT);
$stmt->execute();
echo json_encode([$entry_id]);
知道什么是错的吗?
更新。表
echo '<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Transaction Quote</th>
<th>Action</th>
</tr>
</thead>';
foreach($pdo->query("SELECT * FROM entries ORDER BY entry_id DESC LIMIT 20") as $row){
echo '
<tbody>
<tr>
<td>'.$row['entry_id'].'</td>
<td>'.$row['entry_name'].'</td>
<td>'.$row['entry_transaction_quote'].'</td>
<td><button class="btn btn-sm btn-danger remove-item" ><i class="glyphicon glyphicon-trash"></i> Delete</button></td>
</tr>
</tbody> ';
}
echo ' </table>
答案 0 :(得分:2)
使用类名称
更改 td <td class="entry_id">'.$row['entry_id'].'</td>
jquery
var entry_id = $(this).parent("td").parent("tr").find('.entry_id').text().trim();
答案 1 :(得分:0)
试试这个
Html应该像
<tr entry_id="<?=$row['entry_id']?>">
<td><button class="btn btn-sm btn-danger remove-item" ><i class="glyphicon glyphicon-trash"></i> Delete</button></td>
</tr>
Js代码应该像
var entry_id = $(this).parent().parent().attr("entry_id");