请使用jQuery和ajax从我的网站中删除项目,但是我不知道如何获得我想要删除或更少的特定id,请参见下面的示例:
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
<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>拜托,我不知道如何将我想删除的内容的id传递给ajax,有人能帮我吗?
发布于 2016-06-01 12:17:36
替换你的
var id = $('#file-').val();使用
var id=$(this).attr('id').split("-")[1];顺便说一句,我还没有测试你剩下的代码。特别是用于绑定单击事件的#delete-选择器。
发布于 2016-06-01 12:14:06
更新:
使用数据属性为HTML元素赋值是一种很好的方法。 对于这个HTML和jQuery,两者看起来都类似于下面的内容。
HTML:
<span id="file-3">Orange</span> <a data-fileid="3" class="cmnDeleteFile">Delete</a>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.
});
旧答案:
你走错了路。给每个链接公共CSS类和火灾触发事件点击一个链接像这样。
HTML:
<span id="file-3">Orange</span> <a id="3" class="cmnDeleteFile">Delete</a>JQUERY
$(".cmnDeleteFile").click(function(e){
e.preventDefault();
var id=$(this).attr('id');
// This is how you get id of the file from same element.
});https://stackoverflow.com/questions/37568253
复制相似问题