6

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

私は成功せずに使用しようとしました:

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

1 に答える 1

18

この関数closestは、指定されたセレクターの最初の精度を に与えancestorsます。ラベルは最初の祖先ではなく、親 trchildrenにあります。親 tr に移動し、find を使用してその親を検索する必要がありますlabel

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

また

$(this).closest('tr :td:eq(0)').find('label').html()
于 2012-12-13T05:46:03.090 に答える