Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除父div-jquery_Javascript_Jquery - Fatal编程技术网

Javascript 删除父div-jquery

Javascript 删除父div-jquery,javascript,jquery,Javascript,Jquery,当用户单击delete时,我需要删除父div。到目前为止,我有以下代码: <script> function Confirmation(message, theurl) { if(confirm(message)) { $.ajax({ type: "POST", url: theurl, success:function(response){ //on success, hide

当用户单击delete时,我需要删除父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>

功能确认(消息、URL)
{
如果(确认(消息)){
$.ajax({
类型:“POST”,
url:theurl,
成功:功能(响应){
//成功后,隐藏用户要删除的元素。
$(this.className).unwrap();
},
错误:函数(xhr、ajaxOptions、thrownError){
//出现错误时,我们会提醒用户
警报(thrownError);
}
});
} 
}
测试7
先进的

此代码执行删除,但div仍然没有展开。我也尝试了remove()函数,但仍然没有成功。

这在ajax成功中并没有提到标签。您应该在开始时存储此操作的引用,并使用内部ajax success的引用来执行删除操作

<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);
            }
    });
  } 

});    

MySQL
先进的

MySQL 先进的
$(“.delete”)。单击(函数(e){ var self=这个; var didConfirm=confirm('您确定要删除吗?','http://localhost/v2/index.php/hr/employee/delete/1/skills/2'); 如果(didConfirm==true){ $.ajax({ 类型:“POST”, 网址:图尔, 成功:功能(响应){ //成功后,隐藏用户要删除的元素。 $(self).parent().parent().next('hr').remove() $(self.parent().parent().remove(); }, 错误:函数(xhr、ajaxOptions、thrownError){ //出现错误时,我们会提醒用户 警报(thrownError); } }); } });
我改变了方法。指定了要删除的类并向其添加了单击侦听器。在它里面显示确认警报并进行ajax调用等,我希望这种方法对您有用

您应该尝试$(this).parent().remove(this);而不是$(this.className).unwrap();如果要删除刚刚单击的按钮的父标记,请在Confirmation()函数中执行此操作。


<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>
功能确认(elem、msg){ if(确认('您确定要删除吗?')){ $.post(elem.href) .完成(功能(数据){ $(elem).parents(“.row”).remove(); }).失败(功能(数据){ 警报(数据); }); } } 测试7 先进的

编辑:现在包括失败案例。 编辑2:包括功能的使用

试试这个

$('body').on('click', '.buttonremove', function() {
    $(this).parents("div:first").remove();
});

首先,我不明白为什么要传递消息“您确定要删除吗?”作为参数。确实有更好的方法可以做到这一点,但现在我只更正您的代码,我不能100%确定this指针是否有效

更改此声明

 $(this.className).unwrap();  

如果要使用class=“row”删除div

将语句更改为

$(this).parent().parent().remove();

我认为您的代码是正确的。但是我需要使用Confirmation()函数。downvoter能给我一个有效的理由吗?为什么我对一个简单的问题的答案是:删除一个父项,同时保留它的子项,却被否决了??作品中特别提到了“展开”。你能给我演示一下吗。我尝试了你的代码,但仍然不起作用。我试着保持警惕(这个);结果是“未定义”。这正常吗?我已经升级了,试试那个
 $(this).parent().remove();  
$(this).parent().parent().remove();