2012-12-13 11 views
6

私はユーザーが商品ポップアップオーバーレイを更新して閉じるたびに注文受領書を更新する関数を書いています。 この関数はフォームの各入力を検索し、その値が0より大きい場合、領収書divに情報を追加します。私は、入力フィールドの上にあるラベル要素の.html()を取得しようとしており、現在の項目を説明し、これを領収書の項目の説明として使用します。jQueryの上からラベルを取得

私は成功せず使用して試してみました:ここ

- $this.closest('label').html() 
- $this.prev('label').html() 
- $this.find('label').html() 
- this.element.find('label').html() 

は私のコードです。私が話していますセクションでは、一部...

function updateOrder(){ 

var items = ''; 
$('input').each(function(){ 
    var $this = $(this), 
     i_value = $this.attr('value'); 

    if(i_value > 0){ 
     items += $this.attr('id') + ' Quantity: ' + i_value + '<br/>' 
     + 'label is: ' + $this.closest('label').html() + '<br/>'; 
    } 
}); 

$('#d_reciept').html(items); 

}

とサンプルフォームアイテムのラベルである 'です

<tr> 
<td class="td_thumbs"> 
    <div> 
     <a class="fancybox" data-fancybox-group="vinyl-banners" href="img/products/vinyl-corporateblue.jpg"> <img src="img/products/thumbs/vinyl-corporateblue-thumb.png" alt="vinyl c-blue"/></a> 
     <label>Corporate Blue</label> 
    </div> 
</td> 
<td>6</td> 
<td> 
    <input id="vinyl-blue" type="number" max="6" min="0" value="0"/> 
</td> 
</tr> 

答えて

16

機能closestがあなたに与えられたセレクターの最初のaccuranceを与えますancestorsでは、あなたのラベルは親trではなく、最初の祖先にはありませんchildren、あなたは親trに行き、次にlabelの親を検索するためにfindを使用する必要があります。

$(this).closest('tr').find('label').html() 

または

$(this).closest('tr :td:eq(0)').find('label').html() 
+0

華麗! $(this).closest( 'tr')。find( 'label')。html()worked :) – lukeocom

関連する問題