我想在点击“删除”锚标记后删除相应的行。
从服务器响应我得到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
}
}
});
}
});
最佳答案
您可以改用parents()
遍历到与父级匹配的<tr>
选择器。函数.parent()
只会在DOM树上向上移动,而.parents()
返回完整的父级集合以遍历。
$this.parents("tr").remove();
两种不太可能的方案:
您可能需要像在代码块中的其他位置一样专门引用
jQuery
(而不使用$
):var $this = jQuery(this); // was: $(this)
可能是因为您返回的数据不是预期的格式(整数),所以您可以尝试与字符串'
1'
比较(也许您已经测试过了,并且已经在编写它的方式上进行了工作,比较data
如integer
类型):if( data == '1' ){ //....
关于jquery - 要删除点击删除 anchor 标记的相应行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42425042/