【发布时间】:2012-12-14 16:19:14
【问题描述】:
我正在构建一个页面,供用户输入他们在工作日的开始和结束时间。然后我希望根据他们的选择计算计费时间。不确定这是否可以使用 javascript,因为我尝试了一堆不同的代码,但没有得到我想要的结果。以下是我目前所在的位置。
我是 javascript 新手,非常感谢您提供的任何意见。
谢谢, 安德鲁
<select id="start_time" name="start_time">
<option value="" selected="selected" disabled>Choose start time</option>
<option value="09:00:00">9:00 AM</option>
<option value="09:15:00">9:15 AM</option>
<option value="09:30:00">9:30 AM</option>
<option value="09:45:00">9:45 AM</option>
</select>
<br>
<select name="end_time">
<option value="" selected="selected" disabled>Choose end time</option>
<option value="17:00:00">5:00 PM</option>
<option value="17:15:00">5:15 PM</option>
<option value="17:30:00">5:30 PM</option>
<option value="17:45:00">5:45 PM</option>
</select>
<br>
<select name="mealbreak" id="mealbreak">
<option value="" selected="selected" disabled>Did you stop for lunch?</option>
<option value="00:30:00">Yes, we ate.</option>
<option value="00:00:00">No meal breaks.</option>
</select>
<br>
Billable Hours<input type="text" id="billable_hours" name="billable_hours">
<script>
window.onload = function () {
var time1 = document.getElementById("start_time").options[document.getElementById("start_time").selectedIndex].value;
var time2 = document.getElementById("end_time").options[document.getElementById("end_time").selectedIndex].value;
var mealbreak = document.getElementById("mealbreak").options[document.getElementById("mealbreak").selectedIndex].value;
var billable = time2 - time1 - mealbreak;
billable_hours = document.getElementById('billable_hours');
function () {
billable_hours.value = billable.value;
};
};
</script>
【问题讨论】:
-
你应该包括它做什么/不做什么以及任何错误。对于初学者,它执行 onload,它应该在按钮单击或表单提交中。你也不能这样减去时间,你需要创建 Date 对象。
-
好的,谢谢。我是这个论坛和编码的新手,非常感谢您的意见。
标签: javascript forms option