我需要在用户点击删除时删除父div。到目前为止,我有以下代码:
<script>
function Confirmation(message, theurl)
{
if(confirm(message)) {
$.ajax({
type: "POST",
url: theurl,
success:function(response){
//on success, hide element user wants to delete.
$(this.className).unwrap();
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
}
}
</script>
<div class="row">
<div class="col-sm-6">test7</div>
<div class="col-sm-3">Advanced</div>
<div class="col-sm-3">
<a href="http://localhost/v2/index.php/hr/employee/editSkill/10">
<a href="javascript:Confirmation('Are you sure you want to delete?', 'http://localhost/v2/index.php/hr/employee/delete/1/skills/10');">
</div>
</div>
此代码执行删除但div仍未解包。我也尝试了remove()函数,但仍然没有运气。
答案 0 :(得分:2)
function Confirmation(message, theurl)
{
var $this = this;
if(confirm(message)) {
$.ajax({
type: "POST",
url: theurl,
success:function(response){
//based on the structure on your DOM
$($this).parents('.row').remove();
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
}
}
答案 1 :(得分:0)
这个在ajax成功之内并不是指标签。您应该最初存储此引用,并使用内部ajax成功的引用来执行删除操作。
<div class="row">
<div class="col-sm-6">MySQL</div>
<div class="col-sm-3">Advanced</div>
<div class="col-sm-3">
<a href="http://localhost/v2/index.php/hr/employee/editSkill/3">
EDIT
</a>
<a class="delete" href="javascript:void(0)">
DELETE
</a>
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-6">MySQL</div>
<div class="col-sm-3">Advanced</div>
<div class="col-sm-3">
<a href="http://localhost/v2/index.php/hr/employee/editSkill/3">
EDIT
</a>
<a class="delete" href="javascript:void(0)">
DELETE
</a>
</div>
</div>
<hr>
$(".delete").click(function(e){
var self = this;
var didConfirm = confirm('Are you sure you want to delete?', 'http://localhost/v2/index.php/hr/employee/delete/1/skills/2');
if (didConfirm == true) {
$.ajax({
type: "POST",
url: tourl,
success:function(response){
//on success, hide element user wants to delete.
$(self).parent().parent().next('hr').remove()
$(self).parent().parent().remove();
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
}
});
我改变了方法。指定了一个要删除的类,并为其添加了一个单击侦听器。在其中显示确认提醒并进行ajax调用等,我希望这种方法对你有用。
答案 2 :(得分:0)
你应该尝试$(this).parent()。remove(this);而不是$(this.className).unwrap();如果要删除刚刚单击的按钮的父标记,请在Confirmation()函数内部。
答案 3 :(得分:0)
<script>
function Confirmation(elem, msg) {
if(confirm('Are you sure you want to delete?')){
$.post(elem.href)
.done(function(data){
$(elem).parents(".row").remove();
}).fail(function(data){
alert(data);
});
}
}
</script>
<div class="row">
<div class="col-sm-6">test7</div>
<div class="col-sm-3">Advanced</div>
<div class="col-sm-3">
<a href="http://localhost/v2/index.php/hr/employee/editSkill/10">
<a onclick="Confirmation(this, 'Are you sure you want to delete?'); return false;" href="http://localhost/v2/index.php/hr/employee/delete/1/skills/10">
</div>
编辑:现在包括失败案例。 编辑2:包括使用功能;
答案 4 :(得分:0)
试试这个
$('body').on('click', '.buttonremove', function() {
$(this).parents("div:first").remove();
});
答案 5 :(得分:0)
首先,我不知道你传递信息的原因&#34;你确定要删除吗?&#34;作为一个论点。有更好的方法可以做到这一点,但是现在我只会纠正你的代码,我不能100%确定这个指针是否有效。
更改此声明
$(this.className).unwrap();
到
$(this).parent().remove();
如果你想用class =&#34; row&#34;
删除div将语句更改为
$(this).parent().parent().remove();