2017-02-23 63 views
2

我想删除相应的行上单击“删除”锚标记。想要删除相应的行上点击删除锚标记

从服务器响应我得到1,但是当我试图删除不起作用的锚标记的父项的父项。

这里是代码

由于我的HTML代码是:

      <tr> 
           <td><?php echo $w_value['pro_name']; ?></td> 
           <td><?php echo $w_value['sellingprice']; ?></td> 
           <td><a class="remove_wishlist" href="javascript:;" data-uid="<?php echo $uid;?>" data-pid="<?php echo $w_value['id']; ?>">Remove</td> 
          </tr> 

而且我的Ajax代码是:

jQuery(document).on("click",".remove_wishlist",function(){ 

    if(confirm('Are you sure to remove this item.')){ 

     var pid = jQuery(this).data("pid"); 
     var uid = jQuery(this).data("uid"); 
     var urll = WEBSITE_URL+"pages/removewishlist"; 
     var $this = $(this); 
     $.ajax({ 
      type:'post', 
      url:urll, 
      data:{pid:pid,uid:uid}, 
      success:function(data){ 
       console.log(data); 
       if(data == 1){ 

        $this.parent().parent("tr").remove();//this is not removing the entire row 

       } 
      } 
     }); 



    } 

}); 

回答

1

您可以使用parents()代替遍历回升到父匹配<tr>选择。函数.parent()只会在DOM树上上一级,而.parents()会返回完整的父级集合来遍历。

$this.parents("tr").remove(); 

少了两个可能的情景:

您可能需要参考jQuery明确(而不是使用$)就像你在代码块做到万无一失:

var $this = jQuery(this); // was: $(this)

这可能是你的数据返回不是预期的格式(整数),所以你可以尝试比较字符串'1'(也许你已经测试过这个,它已经工作t他的方式你写的,比较datainteger型):

if(data == '1'){ //.... 
+1

@感谢代码是正确的,问题是在比较数据==“1”是正确的。 –