[英]Delete single item using Ajax and jQuery
Please I need to delete item from my website using jQuery and ajax but I don't know how to get the particular id of what I want to delete or less is single see below example: 请我需要使用jQuery和Ajax从我的网站中删除项目,但我不知道如何获取要删除的特定ID或更少,请参见以下示例:
HTML CODE HTML代码
<span id="file-1">Orange</span> <a id="delete-1">Delete</a>
<span id="file-2">Orange</span> <a id="delete-2">Delete</a>
<span id="file-3">Orange</span> <a id="delete-3">Delete</a>
<span id="file-4">Orange</span> <a id="delete-4">Delete</a>
<span id="file-5">Orange</span> <a id="delete-5">Delete</a>
<!--Next item will have id of 6 is looping...-->
AJAX JQUERY AJAX JQUERY
<script>
$(document).ready(function(e){
$("#delete-").click(function(){
//Am confused here how to know which id need to be deleted?
var id = $('#file-').val();
$.ajax({
url:'/delete_reply.php',
data:'id='+id,
type: "POST",
beforeSend: function(){
$('#comment-'+id'').attr('class', 'deleting');
},
success: function(data){
$('#comment-'+id'').hide();
$(#comment-'+id'').css('display','none');
}
});
});
});
</script>
Please I don't know how to pass the id of the content I want to delete to the ajax can someone help me? 请我不知道如何将要删除的内容的ID传递给Ajax,有人可以帮助我吗?
It's good approach to assign value to HTML element using data attributes . 这是使用data属性为HTML元素分配值的好方法。
For that HTML and jQuery both would look something like follow. 对于该HTML和jQuery来说,它们都看起来像如下。
HTML: HTML:
<span id="file-3">Orange</span> <a data-fileid="3" class="cmnDeleteFile">Delete</a>
JQUERY JQUERY
$(".cmnDeleteFile").click(function(e){
e.preventDefault();
var id=$(this).data('fileid');
// This is how you get id of the file from same element using data attribute.
});
You're following wrong method. 您使用的方法错误。 Give every link common CSS class and fire trigger event on click of a link like this. 单击每个链接,为每个链接提供通用的CSS类并触发事件。
HTML: HTML:
<span id="file-3">Orange</span> <a id="3" class="cmnDeleteFile">Delete</a>
JQUERY JQUERY
$(".cmnDeleteFile").click(function(e){
e.preventDefault();
var id=$(this).attr('id');
// This is how you get id of the file from same element.
});
Replace your 更换您的
var id = $('#file-').val();
with 与
var id=$(this).attr('id').split("-")[1];
Btw, I haven't tested rest of your code. 顺便说一句,我还没有测试其余的代码。 Particularly, your #delete-
selector that you have used for binding click event. 特别是用于绑定click事件的#delete-
选择器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.