jQuery 最接近的 TR 选择
希望有人可以指教。单击链接后尝试删除行时遇到问题。
HTML
<table>
<tr><td>Some Data</td><td><a href="#" class="remove-row>Remove Row</a></td></tr>
<tr><td>Some Data</td><td><a href="#" class="remove-row">Remove Row</a></td></tr>
</table>
现在是 JS
$("a.remove-row").live('click', function(eve){
eve.preventDefault();
$.ajax({
type: 'GET',
url: '/someaction/',
dataType: 'json',
success: function(msg){
if(msg.error){
alert(msg.error);
}else{
$(this).closest('tr').remove();
alert(msg.success);
}
}
})
});
这应该非常简单,但它不会删除该行。只是为了好玩,如果我将其更改为类似的内容,
$('.remove-row').addClass('foo');
它会将 foo 添加到所有表行。所以可以理解为什么它不删除最近的行。
有什么想法吗?
提前致谢。
Hope someone can advise. Having issues trying to remove a row once a link has been clicked.
HTML
<table>
<tr><td>Some Data</td><td><a href="#" class="remove-row>Remove Row</a></td></tr>
<tr><td>Some Data</td><td><a href="#" class="remove-row">Remove Row</a></td></tr>
</table>
Now the JS
$("a.remove-row").live('click', function(eve){
eve.preventDefault();
$.ajax({
type: 'GET',
url: '/someaction/',
dataType: 'json',
success: function(msg){
if(msg.error){
alert(msg.error);
}else{
$(this).closest('tr').remove();
alert(msg.success);
}
}
})
});
This should be real simple buts its not removing the row. Just for kicks if I change it to something like
$('.remove-row').addClass('foo');
It will add foo to all table rows. So can understand why its not removing the closest row.
Any Ideas ?
Thank in advanced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是
this
当前引用了success
回调中的 ajax 对象,但它很容易修复,使用content
选项如下:
context
选项指定 < 中的this
内容code>$.ajax() 回调函数,因为您希望它是您单击的.remove-row
,所以使用this
作为的选项。The problem is
this
currently refers to the ajax object in yoursuccess
callback, but it's an easy fix, use thecontent
option like this:The
context
option dictates whatthis
will be in the$.ajax()
callback functions, since you want it to be the.remove-row
you clicked on, usethis
as the option.尼克的答案应该有效,但你也可以这样做,我不知道哪一个更好,可能是尼克的答案,但无论如何它可能会有所帮助......
Nick's answer should work, but you can do this too, I don't know which one is better, probably Nick's one, but it may help anyway...
您在第一行有未关闭的属性
class="remove-row
。请参阅此处
You have unclosed attribute
class="remove-row
at the first row.See here
提前进行删除/隐藏不是更容易吗?
像这样 :
Wouldn't it be easier to do the remove/hide before hand?
like this :