0
使用php foreach得到如下的html。因此id
就像價格1,價格2,價格3等。jquery如何獲得id的id值(最近的id/id在同一行中)
用戶可更改數量。在這種情況下,我必須將價格乘以更改的數量。我決定用jQuery來做。
首先訪問quantity
。
$('[id^="quantity"]').change(function(){
var quantity = $(this).val();
});
但無法獲得價格的html值。試圖
var price = $(this).find('[id^="price"]').attr('id');
var price = $(this).find('div[id^="price"]').html();
在這兩種情況下,得到了undefined
這裏是HTML代碼的一部分
<div class="shop_cart_box_around">
<div class="shop_cart_price">
<div class="span_cart_name_txt" id="price1">789</div>
</div>
<div class="shop_cart_quantity">
<input type="text" name="quantity[]" id="quantity1" value="3"/>
</div>
</div>
<div class="shop_cart_box_around">
<div class="shop_cart_price">
<div class="span_cart_name_txt" id="price2">54</div>
</div>
<div class="shop_cart_quantity">
<input type="text" name="quantity[]" id="quantity2" value="1"/>
</div>
</div>
這應該得到的價格div的ID,但它不會得到的價格。 – j08691 2014-11-01 18:23:14
var price = $(this).closest(「。shop_cart_box_around」)。find('[id^=「price」]').html();這工作。謝謝! – user2118559 2014-11-01 18:25:06
使用'val()'而不是'html()'輸入元素 – 2014-11-01 18:26:44