看你的形式,
1)我認爲下拉列表總夜晚是多餘的(佔總數的夜晚是從到貨清楚和出發日期)
2)日期(使用它在JavaScript中更簡單)使用數值而不是:'11/05/2013(A)'等。
<select name="ArrivalDate" size="1" id="ArrivalDate">
<option>Please select</option>
<option value="1368399600">13-05-2013</option>
<option value="1368486000">14-05-2013</option>
...
</select>
3)我沒有注意到每晚的價格?也許酒店的名單也可能包含一些ID(如H1A,H1B,H2A,H3A,H3B,H3C,...),而不是文本選項描述
<select name="hotel_choice" id="hotel5">
<option value="nothing" selected="selected">None Selected</option>
<option value='nothing'>.</option>
<option value="h1a">Corinthia Single Room</option>
<option value="h1b">Corinthia Double Room</option>
<option value='nothing'>.</option>
...
</select>
(酒店和房間的),如果你這樣做,那麼JavaScript的可能不是那麼複雜(asuming你做的這些變化和不介意每個酒店在頁面源可見價格):
<script type='text/javascript'>
var prices={nothing:0,h1a:357,h1b:280.50,h2a:380}; //here are your hotel prices
function calculate() {
var days = Math.round((
document.getElementById('datedepart').value -
document.getElementById('ArrivalDate').value
)/86400); //timestamp is in seconds
document.getElementById('total_cost').value =
days *
prices[ document.getElementById('hotel5').value ];
}
</script>
請注意,不存在任何細微在代碼中,它是基於假設,日期更改爲其代表整數值(例如由php函數time()返回)也可能是我在第e你的元素的ID名稱
然後剩下的就是把「calculate();」 javascript函數爲onchange事件的所有控件,你就完成了。
<select name="hotel_choice" id="hotel5" onchange='calculate();'>
...
</select>
和
<select name="ArrivalDate" size="1" id="ArrivalDate" onchange='calculate();'>
...
</select>
,並在出發日期選擇相同。
編輯:
你可以在你的日期選擇器使用日期,但你必須使用類似該字符串parste成若干個客戶端:
var dt=Date.parse(document.getElementById('ArrivalDate').value);
但一定要檢查支持的日期格式這項功能,同時注意它返回的毫秒自1970年以來的號碼,這樣你將不得不通過86400000進行劃分的86400
,而不是ED IT - 檢查日期填寫
function calculate() {
var dd=document.getElementById('datedepart');
var da=document.getElementById('ArrivalDate');
var total=document.getElementById('total_cost');
var hotel=document.getElementById('hotel5');
//no hotel room selected or not depart date set or not arrival date set
//or departing before arrival (nonsense) - set total to ZERO and exit the function
if (!(dd.value*1) || !(da.value*1) || da.value>dd.value) {
total.value='0';//you can set it to 'not allowed' also if you wish (instead of '0')
return;
}
var days = Math.round((
dd.value -
da.value
)/86400); //timestamp is in seconds
var cost = days * prices[ hotel.value ];
if (isNaN(cost))
cost = 0; //or set to "invalid input" - but this line should not be needed at this point
total.value = cost;
}
多少費用一晚? – 2013-03-15 10:59:58
你可以發佈你到目前爲止嘗試過的代碼嗎? – rhughes 2013-03-15 11:17:29
我不知道克里斯布朗代碼! – 2013-03-20 14:02:00