[英]Undefined error getting parents childs input value
I have a table that looks like this: 我有一个看起来像这样的表:
<table>
<tr>
<td class="packing-vol">0.19</td>
<td class="qty-cell"><input type="text" name="qty[]" class="qty" value="2" /></td>
</tr>
<tr>
<td class="packing_total">0.70</td>
<td class="qty-cell"><input type="text" name="qty[]" class="qty" value="1" /></td>
</tr>
</table>
I'm looping through each occurence of .packing-vol getting it's text, then I want to get the qty from the same row so I go up to it's parent then drill into the .qty class. 我正在遍历.packing-vol的每次出现,以获取其文本,然后我想从同一行中获取数量,因此我转到它的父级,然后钻入.qty类。 But when I alert(qty) i get 'undefined' message.
但是当我提醒(数量)时,我得到“未定义”消息。
var total = 0;
jQuery('.packing-vol').each(function(i) {
var qty = jQuery(this).parent("td.qty-cell .qty").val();
alert(qty);
var cur = parseFloat(jQuery(this).text());
if(!isNaN(cur)){
total = total + cur;
}
});
I think you should do this: 我认为您应该这样做:
var qty = jQuery(this).parent().find("td.qty-cell .qty").val();
You need to go 1 level up (using .parent
) and then find your field inside with .find
你需要去1级了(使用
.parent
),然后用里面找到你的领域.find
parent
is just one level upwards. parent
只是向上一级。 You can't use it to go inside trees again. 您不能再使用它进入树木。
parents
is multiple levels upwards. parents
是多个层次的向上。 You can't use it to go inside trees again. 您不能再使用它进入树木。
You can use parent
/ parents
and then use find
to do what you want, or even better: 您可以使用
parent
/ parents
,然后用find
做你想要什么,甚至更好:
var total = 0;
jQuery('.packing-vol').each(function(i) {
var qty = jQuery(this).parent().children('.qty-cell').children('.qty').val();
alert(qty);
var cur = parseFloat(jQuery(this).text());
if (!isNaN(cur)){
total = total + cur;
}
});
You could also use find
, but it is slower than going directly inside, because it has to search the DOM object. 您也可以使用
find
,但是它比直接进入它要慢 ,因为它必须搜索DOM对象。
But you could also do: 但是您也可以这样做:
var qty = jQuery(this).parent().find("td.qty-cell .qty").val();
Instead of 代替
var qty = jQuery(this).parent("td.qty-cell .qty").val();
Try: 尝试:
var qty = jQuery(this).parent().find(".qty").val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.