イベントメソッドを分離して、大きなクラス内の1つの特定の要素だけに影響するようには見えません。クラスの1つの要素に影響を及ぼすJQueryイベントメソッド
目標:夜間の入力ボックスで値を変更すると、合計価格が変更されます(その特定の入力のみ)。現在、すべてのクラスが同時に変更されています。私は信じる.findか.closestはトリックですが、実装方法は不明です。ここには.jsがあります。
//nights auto multiply price
$(".cashtotal").prepend("$");
$('.nights').on('keyup change', function() {
var nights = +$(this).val();
var dailyPrice = +$(this).closest(".tour").data("daily-price");
$(".cashtotal").text(nights * dailyPrice);
$(".nights-total").text(nights);
$(".cashtotal").prepend("$");
});
と質問が答えを与える人は、それを解釈するために必要な方法で言葉で表現されたHTML
<div class="col-md-4 socal tour panel panel-default" data-discount="99" data-daily-price="100">
<h2 class="title">Idlehour, Angeles National Forest</h2>
<div class="info">
<div class="description">A pleasant trail through red fir forest with access to different
lake basins and the Yosemite Creek watershed.</div>
<div class="nightsnum">
<p>
<label for="nights">Number of Nights</label>
</p>
<p>
<input type="number" value="3" class="nights">
</p>
</div>
<div class="total">
<p><span class="cashtotal">400</span><br> for <span class="nights-count">3</span> Nights</p>
</div>
</div>
あなたはすでに$(this).closest( "。tour") 'を使っていますので、' $(this)).closest( "tour")。find( "。cashtotal")にチェーンしてください。 text(nights * dailyPrice); ' – adeneo
Hopelyこの問題は解決しました。 Adeneoは行くあなたに正しい方向を与えている – Franco
@ adeneoおかげだよ!働いた。私は、コメントが回答とどのように違うのかよく分かりません。私はあなたのものを正しいものとして選択したいと思います。 – gwp