简体   繁体   English

jQuery无法删除父div

[英]jQuery fails to remove parent div

I am trying remove parent div when AJAX PHP Script is finished. 我尝试在AJAX PHP脚本完成时删除父div。 Problem is that you when php Script is jQuery fails to remove div. 问题是,当PHP脚本是jQuery时,您无法删除div。

Does someone knows where's error in my code and why I can't remove wanted DIV with methods that I provided? 有人知道我的代码中的错误以及为什么我无法使用我提供的方法删除想要的DIV吗?

Here is code: 这是代码:

HTML: HTML:

<div id="newTask">  
<input type="hidden" id="taskID" 1="" name="taskID" value="39">
<input type="text" id="taskName1" name="taskName1" value="12">  
<span class="remove" onclick="removeDIVS(39);"></span>
</div>

JS: JS:

 function removeDIVS(currentID){

    $.ajax({
      url: 'scripts/removeTask.php',
      type: 'post',
      data: 'id='+currentID,

      success: function(data, status) {
        if(data == "OK") {
            $(this).parent().remove();
        }
        else{
                  $('#TaskResult').html(data);
        }
      },
      error: function(xhr, desc, err) {
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
      }
    }); // end ajax call
  }

try: 尝试:

HTML: HTML:

<div id="newTask">  
<input type="hidden" id="taskID" 1="" name="taskID" value="39">
<input type="text" id="taskName1" name="taskName1" value="12">  
<span class="remove" data-myid="39"></span>
</div>

JS: JS:

$(".remove").click(function(){
var currentID = $(this).data('myid');
var parenID = $(this).parent().attr('id');
 $.ajax({
      url: 'scripts/removeTask.php',
      type: 'post',
      data: 'id='+currentID,

      success: function(data, status) {
        if(data == "OK") {
            $("#"+parenID).remove();
        }
        else{
                  $('#TaskResult').html(data);
        }
      },
      error: function(xhr, desc, err) {
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
      }
    }); // end ajax call
})

Use 采用

$("#newTask").remove();

instead of 代替

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

because here $(this) is not what you are expecting. 因为在这里$(this)不是您所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM