我正在构建一个页面供用户在工作日输入他们的开始时间和结束时间。然后我希望根据他们的选择计算计费时间。不知道这是否可能与JavaScript,因为我已经尝试了一堆不同的代码没有我想要的结果。以下是我目前的位置。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对象。 – MrCode
是的,谢谢。我是新来的这个论坛以及编码,所以非常欣赏这个输入。 – user1882674