任何人都可以幫我解決這個問題嗎?在jQuery中使用'this'
這是我的HTML
<div class="wrapper">
<div class="vacation" data-price="339.99">
<h3>British vacation</h3>
<p>£339.99 per ticket</p>
<p>Tickets:
<input type="number" class="quantity" value='1'>
<p/>
</div>
<p>Total price £: <span class="total">399.99</span></p>
</div>
<div class="wrapper">
<div class="vacation" data-price="449.99">
<h3>Spanish vacation</h3>
<p>£449.99 per ticket</p>
<p>Tickets:
<input type="number" class="quantity" value='1'>
<p/>
</div>
<p>Total price £: <span class="total">449.99</span></p>
</div>
<div class="wrapper">
<div class="vacation" data-price="559.99">
<h3>Italian vacation</h3>
<p>£559.99 per ticket</p>
<p>Tickets:
<input type="number" class="quantity" value='1'>
<p/>
</div>
<p>Total price £: <span class="total">559.99</span></p>
</div>
和jQuery
$(document).ready(function(){
$('.vacation').on('keyup', '.quantity', function() {
var price = +$(this).closest('.vacation').data('price');
var quantity = +$(this).closest('.quantity').val();
var totalPrice = (price * quantity).toFixed(2);
$('.total').text(totalPrice);
});
});
的問題是,當我計算價格的度假選擇一個我已經得到了所有的總價格更新。 我試過:
$(this).closest('.total').text(totalPrice);
而且它不工作。
請檢查它JS Bin
非常感謝!