jQuery 最接近的 TR 选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3548762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:38:24  来源:igfitidea点击:

jQuery closest TR selection

jqueryjquery-selectorsclosest

提问by Lee

Hope someone can advise. Having issues trying to remove a row once a link has been clicked.

希望有人能指教。单击链接后尝试删除行时遇到问题。

HTML

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

现在的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.

它会将 foo 添加到所有表行。所以可以理解为什么它不删除最近的行。

Any Ideas ?

有任何想法吗 ?

Thank in advanced.

先谢谢了。

回答by Nick Craver

The problem is thiscurrently refers to the ajax object in your successcallback, but it's an easy fix, use the contentoption like this:

问题this当前是指success回调中的 ajax 对象,但这是一个简单的修复,使用如下content选项:

 $("a.remove-row").live('click', function(eve){
  eve.preventDefault();
  $.ajax({
   context: this,                    //add this here!
   type: 'GET',
   url: '/someaction/',
   dataType: 'json',
   success: function(msg){
    if(msg.error){
     alert(msg.error);
    }else{
     $(this).closest('tr').remove();
     alert(msg.success);
    }    
   }
  })
 });

The contextoption dictates what thiswill be in the $.ajax()callback functions, since you want it to be the .remove-rowyou clicked on, use thisas the option.

context选项决定this$.ajax()回调函数中的内容,因为您希望它是.remove-row您单击的那个,this用作选项。

回答by NicolasT

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...

尼克的答案应该有效,但你也可以这样做,我不知道哪个更好,可能是尼克的答案,但无论如何它可能会有所帮助......

$("a.remove-row").live('click', function(eve){
  var row = this;
  eve.preventDefault();
  $.ajax({
   type: 'GET',
   url: '/someaction/',
   dataType: 'json',
   success: function(msg){
    if(msg.error){
     alert(msg.error);
    }else{
     $(row).closest('tr').remove();
     alert(msg.success);
    }    
   }
  })
 });

回答by Tyler

Wouldn't it be easier to do the remove/hide before hand?

事先进行删除/隐藏不是更容易吗?

like this :

像这样 :

$("a.remove-row").live('click', function(eve){
      $(this).hide();
      <The rest of your code logic>
         ......

回答by fantactuka

You have unclosed attribute class="remove-rowat the first row.

class="remove-row在第一行有未关闭的属性。

See here

看这里