我想在函数sumMon1完成后调用函数sumWH。这两个函数都从标签获取文本,对它们的值求和,并更新计数器标签。我试图在sumMon1之后调用sumHW的代码在下面,并且不起作用。
$.when(sumMon1()).done(function () {
sumWH();
});
求和函数如下:
<script>
$.when(sumMon1()).done(function () {
sumWH();
});
function sumWH() {
alert(event.target);
//Get Friday AM Out Value
var a = parseInt($('[id$="lblWeek1FridayTotalHrs"]').val(), 10) || 0;
//Get Friday AM IN Value
var b = parseInt($('[id$="lblWeek2FridayTotalHrs"]').val(), 10) || 0;
//Get Friday PM Out Value
var c = parseInt($('[id$="lblWeek1SaturdayTotalHrs"]').val(), 10) || 0;
//Get Friday AM IN VALUE
var d = parseInt($('[id$="lblWeek2SaturdayTotalHrs"]').val(), 10) || 0;
//Get Friday AM Out Value
var e = parseInt($('[id$="lblWeek1SundayTotalHrs"]').val(), 10) || 0;
//Get Friday AM IN Value
var f = parseInt($('[id$="lblWeek2SundayTotalHrs"]').val(), 10) || 0;
//Get Friday PM Out Value
var g = parseFloat($('[id$="lblWeek1MondayTotalHrs"]').val(), 10);
//Get Friday AM IN VALUE
var h = parseInt($('[id$="lblWeek2MondayTotalHrs"]').val(), 10) || 0;
//Get Friday AM Out Value
var i = parseInt($('[id$="lblWeek1TuesdayTotalHrs"]').val(), 10) || 0;
//Get Friday AM IN Value
var j = parseInt($('[id$="lblWeek2TuesdayTotalHrs"]').val(), 10) || 0;
//Get Friday PM Out Value
var k = parseInt($('[id$="lblWeek2WednesdayTotalHrs"]').val(), 10) || 0;
//Get Friday AM IN VALUE
var l = parseInt($('[id$="lblWeek1WednesdayTotalHrs"]').val(), 10) || 0;
//Get Friday AM Out Value
var m = parseInt($('[id$="lblWeek2ThursdayTotalHrs"]').val(), 10) || 0;
//Get Friday AM IN Value
var n = parseInt($('[id$="lblWeek1ThursdayTotalHrs"]').val(), 10) || 0;
var result = a + b + c + d + e + f + g + h + i + j + k + l + m + n;
if (result > 0 && result < 81 && !isNaN(result)) {
$('[id$="txtTotalHours"]').html(result);
} else if (result == 0) {
$('[id$="txtTotalHours"]').html(0);
} else if (isNaN(result)) {
$('[id$="txtTotalHours"]').html(result);
}
}
</script>
<script>
$(document).ready(function () {
sumMon1();
$('[id$="txtWeek1MondayPM_OUT"], [id$="txtWeek1MondayAM_OUT"]').on("blur", function () {
sumMon1();
});
});
function sumMon1() {
//Get Monday AM Out Value
var Out2Dec = $('[id$="txtWeek1MondayAM_OUT"]').val();
//split dec and whole AM OUT
var OutAmDec = Out2Dec % 1;
var OutAMWhole = Math.floor(Out2Dec);
if (OutAMWhole < 12 && OutAMWhole != 0 && !isNaN(OutAMWhole)) {
OutAMWhole += 12;
}
//Get Monday AM IN Value
var In2Dec = $('[id$="txtWeek1MondayAM_IN"]').val();
//split dec and whole AM IN
var InAmDec = In2Dec % 1;
var InAmWhole = Math.floor(In2Dec);
//Get Monday PM Out Value
var Out1Dec = $('[id$="txtWeek1MondayPM_OUT"]').val();
//split dec and whole PM OUT
var OutPmDec = Out1Dec % 1;
var OutPMWhole = Math.floor(Out1Dec);
if (OutAMWhole < 12 && OutAMWhole != 0 && !isNaN(OutAMWhole)) {
OutPMWhole += 12;
}
//Get Monday AM IN VALUE
var In1Dec = $('[id$="txtWeek1MondayPM_IN"]').val();
//split dec and whole PM IN
var InPmDec = In1Dec % 1;
var InPMWhole = Math.floor(In1Dec);
//calculate times
var InAmVal = (InAmWhole * 60) + (InAmDec * 100);
var OutAmVal = (OutAMWhole * 60) + (OutAmDec * 100);
var InPmVal = (InPMWhole * 60) + (InPmDec * 100);
var OutPmVal = (OutPMWhole * 60) + (OutPmDec * 100);
var Difference = (OutAmVal - InAmVal) + (OutPmVal - InPmVal);
var result = Difference / 60;
//display result
if (result > 0 && !isNaN(result)) {
$('[id$="lblWeek1MondayTotalHrs"]').html(result.toFixed(2));
} else {
var value2 = (result.toFixed(2) * -1);
$('[id$="lblWeek1MondayTotalHrs"]').html(value2);
}
}
</script>
sum函数位于单独的脚本标记中。 sumMon1设置为使一系列文本框变得模糊。
这是使此功能起作用的一种方法,还是有一种更好的方法来实现此功能?
答案 0 :(得分:0)
jQuery.when
用于异步功能。
异步类似于setInterval
或jQuery.ajax
。
除了异步内容外,JavaScript(或几乎所有其他语言)都按顺序从上到下运行代码。
您的代码未使用异步事物。简单地做
sumMon();
sumWH();