提问人:Marcos Almeida 提问时间:6/27/2023 最后编辑:DavidMarcos Almeida 更新时间:6/27/2023 访问量:36
如何在ajax调用删除MySQL行后通过ID删除UL LI?不确定在 I Put (#liid).remove with jquery 的什么位置
how to remove a ul li by id, after a ajax call to delete a MySQL row? not sure where in the i put (#liid).remove with jquery
问:
我有一个应用程序,可以显示收入项目列表,其中每个项目都在 id = 的 li 中 到目前为止,我有一个代码,当用户单击垃圾桶图标时调用AJAX,该图标使用AJAX调用的PHP脚本删除MySQL行,但是当我单击垃圾桶图标时,如果不刷新页面,则我无法删除具有唯一ID的LI
这是我目前拥有的一段代码
<ul>
<?php $sqlinc = "SELECT * FROM transactions WHERE username = '$username' AND transaction = 'income'";
$resultinc = mysqli_query($conn, $sqlinc);
if (mysqli_num_rows($resultinc) > 0) {
// output data of each row
while($rowinc = mysqli_fetch_assoc($resultinc)) {
echo '<li class="delli" id="'.$rowinc['id'].'">
<button id="" class="fl delbutton" onclick="">'. $rowinc['memo'].'</button>
<span class="fr">
<button>'. $rowinc['amount'].'</button>
<button class="delbutton" id="'.$rowinc['id'].'">
<i class="fa fa-trash">'.$rowinc['id'].'</i>
</button>
</span>
</li> '; }}?>
<!-- <li>
<button class="fl" onclick="changeIncomeName()">name</button>
<span class="fr">
<button>100</button>
<button data-delete="item">
<i class="fa fa-trash"></i>
</button>
</span>
</li> -->
</ul>
<!-- 'script to remove income row from mysql' section -->
<script>
function aler() {
alert("Transaction submited!");
</script>
<!-- 'delete mysql row' section -->
<script type="text/javascript">
$(document).ready(function() {
$('.delbutton').click(function() {
var id = $(this).attr("id");
if (confirm("Are you sure you want to delete this Member?")) {
$.ajax({
type: "POST",
url: "delete_entry.php",
data: ({
id: id
}),
cache: false,
success: function(html) {
$('#id li').remove()
}
});
} else {
return false;
}
});
});
</script>
<!-- 'end of planned page income list' section -->
但它只有在我刷新页面后才会删除 li
答:
1赞
redystum
6/27/2023
#1
正如@david所说,问题出在javascript中,代码:
$.ajax({
type: "POST",
url: "delete_entry.php",
data: ({
id: id
}),
cache: false,
success: function (html) {
$('#id li').remove()
}
});
正在拾取元素:
<element id="id">
<li>...</li>
</element>
其中不存在,正确的应该是例如:
$.ajax({
type: "POST",
url: "delete_entry.php",
data: ({
id: id
}),
cache: false,
success: function (html) {
$(`#${id}`).remove()
}
});
此代码搜索并删除元素,例如 id=46:
<li id="46">
...
</li>
评论
$('#id li').remove()
#id li
id
#id
'#id li'
id
'#id li'
<li>
id="id"