2013-03-11 3 views
0

그래서 사용자가 추가 할 때 가격을 업데이트하기 위해 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("&euro;" + newprice.toFixed(2)); 


    }); 

}); 

내가 그것을 명확하게 그것을 조금 언급을 시도했다 : 당신은 내가 정확히 볼 수 있도록

은 여기입니다. 자식 시트를 처리하는 이와 같은 코드가 있습니다.

내 문제는 첫 번째 childseat입니다 또는 부스터 좌석 무료입니다. 위에서 볼 수 있듯이

}else if ((numberofboostseat == 1 || numberofboostseat == 0) && (numberofchildseat == "undefined" || numberofchildseat == 0)){ 

이 변수 boostcost는 좌석의 비용을 계산 :

나는이 시도. 다른 코드 덩어리 인 childseat도 이와 비슷한 전역 변수를 사용할 수 있습니다.

누구나 효과적으로 첫 번째 자식 또는 부스터 시트를 무료로 만들 수있는 if 문을 제안 할 수 있습니까?

+0

왜 '번호 (x)'를 사용합니까? – Fresheyeball

+0

변수를 숫자로 취급하는 것이 안전한 방법이라고 생각한 적이 있습니까? 나는 아무 JQuery 전문가도 아니지만 ... – Bohdi

+0

jQuery와 무슨 상관이 있습니까? 어쨌든 변수가 아니라 숫자 리터럴과 함께 사용했습니다. – Barmar

답변

1

자매 varialbe가 감소되지 않도록 변수가 감소 된 후에 변수에 .discount = true을 추가합니다. 이 방법으로 numberofchildseat 또는 numberofboostseat는 1로 할인되지만 둘 다 할인되지 않습니다. .discounted이 설정되어 있지 않으면 자바 스크립트는 false으로 평가합니다.

if(!numberofchildseat.discounted && numberofboostseat > 0){ 
     numberofboostseat--; 
     numberofboostseat.discounted = true; 
    } 
    if(!numberofboostseat.discounted && numberofchildseat > 0){ 
     numberofchildseat--; 
     numberofchildseat.discounded = true; 
    } 
+0

이것이 어리석은 질문 인 경우에 나는 진짜로 유감 스럽, 그러나이 선 numberofchildseat--; 정확히 무엇을합니까? – Bohdi

+1

'foo ++'의 반대입니다. 'numberofchildseat = numberofchildseat-1;과 동일합니다. – Fresheyeball

+0

이 도구가 효과가 있습니까? – Fresheyeball

관련 문제