2017-02-10 43 views
-1

我試圖根據選擇的人數計算座位的成本,問題是第一個座位的成本與其餘的不同。根據輸入因子計算變量的值

第一= 2250個 額外的座位都在995

收費應該是什麼我的語法和計算是什麼?

//seats variable 

var costOfSeats; 


//Calculate cost of seats and display cost of seats 

$('#range--seats').on("change", function() { 

if ($(this).val(1)) { 
    costOfSeats == 2250; 
} else { 
    costOfSeats == costOfSeats + ($(this).val(-1) * 995);// problem here 
} 

$('.output--seats').val("£" + costOfSeats); 
}).trigger("change"); 

回答

1
var n = 3; //number of seats 
var sum = 0; 
var extraSeat = 995; 
var seat = 2250; 

if (n != 0) { 
    sum = seat + ((n-1) * extraSeat) 
} 
+0

可以爲'n' ...'var n = $('#range - seats')。val(); //座位數量? – Amesey

+0

是的,正確的使用 –

+0

雖然這段代碼可能會回答這個問題,但提供關於如何解決問題和/或爲什麼解決問題的額外上下文會提高答案的長期價值。 –

0
if ($(this).val(1)) { 
    costOfSeats == 2250; 
} else { 
    costOfSeats == costOfSeats + ($(this).val(-1) * 995);// problem here 
} 

您使用costOfSeats == costOfSeats,而不是costOfSeats = costOfSeats。您正在比較值而不是分配它們