簡體   English   中英

jQuery無法刪除父div

[英]jQuery fails to remove parent div

我嘗試在AJAX PHP腳本完成時刪除父div。 問題是,當PHP腳本是jQuery時,您無法刪除div。

有人知道我的代碼中的錯誤以及為什么我無法使用我提供的方法刪除想要的DIV嗎?

這是代碼:

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:

 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
  }

嘗試:

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:

$(".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
})

采用

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

代替

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

因為在這里$(this)不是您所期望的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM