简体   繁体   English

使用jQuery最接近的方法查找表行不起作用

[英]Finding a table row using jquery closest method not working

I m trying to get the row of a table using jquery closest method as below 我正在尝试使用jquery最接近的方法获取表的行,如下所示

function GetPackageSizeDescription() {
var row = $(this).closest('tr');
.....
//ajax call
data: { packageId: $(row).find('.packageId').val()}
}    

but after jquery executes this line of code var row = $(this).closest('tr'); 但在jquery执行此行代码后, var row = $(this).closest('tr'); the following line appears in the browser developer tools and I m unable to get the values of elements in that row. 下面的行出现在浏览器开发人员工具中,我无法获取该行中元素的值。 row = [prevObject: jQuery.fn.jQuery.init[1], context: undefined]

Any ideas? 有任何想法吗?

The button element table cell looks like 按钮元素表单元格看起来像

 <td><button type="button" class="btn btn-sm btn-primary" onclick="GetPackageSizeDescription(); return false;">
     <span class="glyphicon glyphicon-info-sign"></span>
      </button>
  </td>

You need to send this through to the function as a parameter. 您需要this作为参数发送给函数。

change to: 改成:

function GetPackageSizeDescription(element) {
var row = $(element).closest('tr');

and when you call GetPackageSizeDescription do: 当您调用GetPackageSizeDescription时,请GetPackageSizeDescription操作:

GetPackageSizeDescription(this)

Or you can refactor your code to this... 或者您可以将代码重构为此...

HTML 的HTML

<td><button id="getpackagesize-btn" type="button" class="btn btn-sm btn-primary">
     <span class="glyphicon glyphicon-info-sign"></span>
      </button>
</td>

JQUERY JQUERY

$('#getpackagesize-btn').on('click', function() {

   var row = $(this).closest('tr');
   ...

   //ajax call
   data: { packageId: $(row).find('.packageId').val()}

   return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM