、その後、合計で何をしますか? 私の例では、私は単純にすべての総価格をまとめて...それが動作するかどうかを判断するまでです。その最後を行うには
$(document).on('change', '.tp-sel', function() {
var index = $(this).index('.tp-sel');
console.log(index);
var oSel = $(this).find('option:selected');
var pair_price = oSel.length ? parseFloat(oSel.data("price")) : 0;
var oQty = $(this).next(".tp-sel-count").find('option:selected');
var pair_count = oQty.length ? parseFloat(oQty.data("count")) : 0;
var totalPair_price = pair_price * pair_count; // of this pair
console.log("Price: " + pair_price + "Count:" + pair_count, totalPair_price);
$("#price").html(totalPair_price.toFixed(2));
});
:あなたは、個々のペアの変更でこれを実行したい場合は
function countPrice() {
var total_price = 0.00;
var data_price = "";
var data_count = "";
$(".tp-sel").each(function() {
var oSel = $(this).find('option:selected');
var pair_price = oSel.length ? parseFloat(oSel.data("price")) : 0;
var oQty = $(this).next(".tp-sel-count").find('option:selected');
var pair_count = oQty.length ? parseFloat(oQty.data("count")) : 0;
var totalPair_price = pair_price * pair_count; // of this pair
console.log("Price: " + pair_price + "Count:" + pair_count,totalPair_price);
total_price += totalPair_price;
});
$("#price").html(total_price.toFixed(2));
}
今、あなたはこのような何かを行うことができます - (合計これは本当に価格は全てカウントされます)いずれかのプルダウンの変更時:
$(document).on('change', '.tp-sel', function() {
var index = $(this).index('.tp-sel');
console.log(index);
calcThis(index);
});
$(document).on('change', '.tp-sel-count', function() {
var index = $(this).index('.tp-sel-count');
console.log(index);
calcThis(index);
});
function calcThis(index) {
var sel = $('.tp-sel').eq(index);
console.log(index);
var oSel = sel.find('option:selected');
var pair_price = oSel.length ? parseFloat(oSel.data("price")) : 0;
var oQty = sel.next(".tp-sel-count").find('option:selected');
var pair_count = oQty.length ? parseFloat(oQty.data("count")) : 0;
var totalPair_price = pair_price * pair_count; // of this pair
console.log("Price: " + pair_price + "Count:" + pair_count, totalPair_price);
$("#price").html(totalPair_price.toFixed(2));
}
ありがとうございます!それは簡単でした! – Cheslav