그래서 사용자가 추가 할 때 가격을 업데이트하기 위해 JQuery를 작성했습니다. Booster Seats와 Child Seats를위한 코드가 하나 있습니다.JQuery if, 첫 번째 무료 로직
$('.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));
});
});
내가 그것을 명확하게 그것을 조금 언급을 시도했다 : 당신은 내가 정확히 볼 수 있도록
은 여기입니다. 자식 시트를 처리하는 이와 같은 코드가 있습니다.
내 문제는 첫 번째 childseat입니다 또는 부스터 좌석 무료입니다. 위에서 볼 수 있듯이
}else if ((numberofboostseat == 1 || numberofboostseat == 0) && (numberofchildseat == "undefined" || numberofchildseat == 0)){
이 변수 boostcost는 좌석의 비용을 계산 :
나는이 시도. 다른 코드 덩어리 인 childseat도 이와 비슷한 전역 변수를 사용할 수 있습니다.누구나 효과적으로 첫 번째 자식 또는 부스터 시트를 무료로 만들 수있는 if 문을 제안 할 수 있습니까?
왜 '번호 (x)'를 사용합니까? – Fresheyeball
변수를 숫자로 취급하는 것이 안전한 방법이라고 생각한 적이 있습니까? 나는 아무 JQuery 전문가도 아니지만 ... – Bohdi
jQuery와 무슨 상관이 있습니까? 어쨌든 변수가 아니라 숫자 리터럴과 함께 사용했습니다. – Barmar