看你的形式,
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