我正在为我的组织制作一个html / javascript时间表,虽然我已经让它正确地进行了计算,但似乎并没有始终如一地进行计算。我不确定问题是什么。
每个输入执行此操作:
<input type="text" name="date-1-out" id="date-1-out" size="5" placeholder="" maxlength="5" onchange="date1calc()" />
因此,对于date-1-out,date-1-lunch,date-1-sick等,每个都设置为在输入更改时运行date1calc()函数。当我选择date-1-out时,它会给我一个准确的小计(超时减去时间),但是当我选择date-1-lunch,bonus,sick,vacation或holiday的值时,它不会更新总数。但是如果我选择date-1-out并选择不同的输出时间,它将使用正确的数字更新总数和小计。
这是javascript:
// Convert hh:mm[am/pm] to minutes
function timeStringToMins(s) {
s = s.split(':');
return s[0]*60 + parseInt(s[1]) + (/pm$/i.test(s[1])? 720 : 0);
}
// Return difference between two times in hh:mm[am/pm] format as hh:mm
function getTimeDifferenceSubtotal(timein, timeout, lunch) {
// Small helper function to pad single digits
function z(n){return (n<10?'0':'') + n;}
// Get difference in minutes
var subtotal = timeStringToMins(timeout) - timeStringToMins(timein) - timeStringToMins(lunch);
// Format difference as hh:mm and return
return z(subtotal/60 | 0) + ':' + z(subtotal % 60);
}
// Return difference between two times in hh:mm[am/pm] format as hh:mm
function getTimeDifference(timein, timeout, lunch, bonus, sick, vacation, holiday) {
// Small helper function to pad single digits
function z(n){return (n<10?'0':'') + n;}
// Get difference in minutes
var total = timeStringToMins(timeout) - timeStringToMins(timein) - timeStringToMins(lunch) - timeStringToMins(bonus) - timeStringToMins(sick) - timeStringToMins(vacation) - timeStringToMins(holiday);
// Format difference as hh:mm and return
return z(total/60 | 0) + ':' + z(total % 60);
}
// Run this function every time a date-1 cell is changed
function date1calc() {
if (document.getElementById("date-1-in").value == '') {var timein = '00:00';} else {var timein = document.getElementById("date-1-in").value;}
if (document.getElementById("date-1-out").value == '') {var timeout = '00:00';} else {var timeout = document.getElementById("date-1-out").value;}
if (document.getElementById("date-1-lunch").value == '') {var lunch = '00:00';} else {var lunch = document.getElementById("date-1-lunch").value;}
if (document.getElementById("date-1-bonus").value == '') {var bonus = '00:00';} else {var bonus = document.getElementById("date-1-bonus").value;}
if (document.getElementById("date-1-sick").value == '') {var sick = '00:00';} else {var sick = document.getElementById("date-1-sick").value;}
if (document.getElementById("date-1-vacation").value == '') {var vacation = '00:00';} else {var vacation = document.getElementById("date-1-vacation").value;}
if (document.getElementById("date-1-holiday").value == '') {var holiday = '00:00';} else {var holiday = document.getElementById("date-1-holiday").value;}
document.getElementById("date-1-subtotal").value = getTimeDifferenceSubtotal(timein, timeout, lunch);
document.getElementById("date-1-total").value = getTimeDifference(timein, timeout, lunch, bonus, sick, vacation, holiday);
}
每次运行相同的功能是不是很糟糕?或者我错过了其他地方的东西?或者它失败了,因为我每次都运行相同的date1calc()脚本?
编辑:如果我将输入中的HTML从“onchange ='date1calc()'”改为“onblur ='date1calc()'”,午餐,奖金,病假,假期和假日领域。是否有“onchange”功能,我没有得到?
编辑2:这可能是一个HTML问题,使用onchange和jquery自动完成功能来提供一个自动下拉菜单
答案 0 :(得分:0)
我认为问题是你的字段是空白的,直到它们被填充然后改变才会经历变化事件。如果在页面初始化时将默认值设置为0:00,则用户更改值,这将触发更新。