わかりましたので、ユーザーが追加料金を追加したときに価格をライブで更新するための JQuery をいくつか書きます。ブースター シート用とチャイルド シート用のコードのチャンクが 1 つずつあります。
これがその1つで、私が何をしているかを正確に見ることができます:
$('.boostseat').change(function() {
//sets the var id as the id of the select box
var id = this.id;
//sets this as the value of the select box
numberofboostseat = $(this).val();
//shows a confirmation message
$('#boostseatadd-'+id).show();
//this handles if an item is removed, so that the value doesn't reset
if (typeof(boosttotal) != "undefined" && boosttotal !== null) {
price = $("#hiddenprice").val() - boosttotal;
}else{
price = $("#hiddenprice").val();
}
//number of days you hire it for cariable
numofdays = "<?php echo $length->days; ?>";
if (numofdays > 4){
//alert ("boost if1");
boostcost = Number(20);
}else if ((numberofboostseat == 1 || numberofboostseat == 0) && (numberofchildseat == "undefined" || numberofchildseat == 0)){
//alert ("boost if2");
boostcost = Number(3) * Number(0);
}else if (numofdays < 1){
//alert ("boost if3");
boostcost = Number(3) * Number(1);
}else{
//alert ("boost if4");
boostcost = Number(3) * Number(numofdays);
}
// determines final price
boosttotal = Number(numberofboostseat) * Number(boostcost);
//more varaibles
newprice = Number(price) + Number(boosttotal);
//updates hiddden fields
$("input#hiddenboostprice").val(boosttotal);
$("input#hiddenboostnum").val(numberofboostseat);
$("input#hiddenprice").val(newprice);
oldnumofboost = $("input#hiddenboostnum").val(numberofboostseat);
//updates the HTML (Live Price Update) as decimal
$('#'+id).html("€" + newprice.toFixed(2));
});
});
わかりやすくするために、少しコメントしてみました。チャイルド シートを処理する、このようなコードの別のチャンクがあります。
私の問題は、最初のチャイルドシートまたはブースターシートが無料であることです.
私はこれを試しました:
}else if ((numberofboostseat == 1 || numberofboostseat == 0) && (numberofchildseat == "undefined" || numberofchildseat == 0)){
上記のように、変数 boostcost はシートのコストをカウントします。コードの他のチャンクであるチャイルドシートにも、これらと同様に使用できるグローバル変数があります。
最初の子またはブースター シートを効果的に無料にする if ステートメントを提案できる人はいますか?