未检索到输入字段的值:未定义的对象

Value of input field not retrieved : undefined object

本文关键字:未定义 对象 字段 检索 输入      更新时间:2024-04-10

我有以下HTML:

<table>
... some other rows
<tr>
   <td>
      <input id="Products_0__Quantity" class="product-quantity" type="text" />
  </td>
  <td>
     ...Some other columns
  </td>
</tr>
... some other rows
</table>

我为每一行中包含的链接附加了一个事件处理程序。我知道它是有效的,因为事件处理程序函数的其他部分工作得很好。然而,这部分没有:

var tr = $(this).closest('tr');
var productQuantity = tr.children('input.product-quantity').val();

此行之后,productQuantity仍然未定义。

怎么了?

使用.find()而不是.children()

.children()方法与.find()的不同之处在于,.children()只在DOM树中向下移动一个级别,而.find()也可以向下移动多个级别来选择子元素(孙元素等)。

代码

tr.find('input.product-quantity').val()

请尝试这个:-

tr.find('input.product-quantity').val();

代替Children

在我看来,$(this)的定义出了问题,你确定你用$得到的元素吗?

不过.find()比children要好。