誰でも助けてくれますか?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);
そして、それは働いていません。
おかげでたくさんのそれをチェックしてください!それはクラス.total
を持つすべての要素のテキストを変更します
$('.total').text(totalPrice);
: