簡體   English   中英

獲取父母孩子的輸入值時發生不確定的錯誤

[英]Undefined error getting parents childs input value

我有一個看起來像這樣的表:

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

我正在遍歷.packing-vol的每次出現,以獲取其文本,然后我想從同一行中獲取數量,因此我轉到它的父級,然后鑽入.qty類。 但是當我提醒(數量)時,我得到“未定義”消息。

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;
    }

});

我認為您應該這樣做:

var qty = jQuery(this).parent().find("td.qty-cell .qty").val();

你需要去1級了(使用.parent ),然后用里面找到你的領域.find

parent只是向上一級。 您不能再使用它進入樹木。

parents是多個層次的向上。 您不能再使用它進入樹木。

您可以使用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;
    }
});

您也可以使用find ,但是它比直接進入它要 ,因為它必須搜索DOM對象。

但是您也可以這樣做:

var qty = jQuery(this).parent().find("td.qty-cell .qty").val();

代替

var qty = jQuery(this).parent("td.qty-cell .qty").val();

嘗試:

var qty = jQuery(this).parent().find(".qty").val();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM