【发布时间】:2021-11-01 15:08:32
【问题描述】:
如何在另一个任务完成后调用一个函数。 在 each(function () 完成后,我正在尝试更新输入框值。
我有一个多行的 html 表。它的“HolidayEnt”列使用来自每行中控制器函数“GetSystemCalculatedHolidayByDate”的值进行更新。在表的每一行中的“HolidayEnt”列中更新之后[更新所有行之后],我必须将 netMinutes 变量中的值更新到表外的输入框 totmins 控件中。但是即使在每个循环完成后,“totmins”列也会第一次更新为 0
这里是代码
var netMinutes = 0.00;
$("#btnCalculate").click(function () {
var netMinutes = 0.00;
var eachMinutes = 0.00;
$('#tblEntry tbody tr').each(function () {
var tr = $(this).closest('tr');
var sunHrs = tr.find("#SundayNetHrs").val();
var monHrs = tr.find("#MondayNetHrs").val();
var tueHrs = tr.find("#TuesdayNetHrs").val();
var wedHrs = tr.find("#WednesdayNetHrs").val();
var thuHrs = tr.find("#ThursdayNetHrs").val();
var friHrs = tr.find("#FridayNetHrs").val();
var satHrs = tr.find("#SaturdayNetHrs").val();
var netHrs = tr.find("#NetHrsPerWeek").val();
var fromDate = tr.find("#FromDate").val();
var toDate = tr.find("#ToDate").val();
$.ajax({
type: "GET",
url: "/Employee/GetSystemCalculatedHolidayByDate?EID=" + $("#EmployeeID option:selected").val()
+ "&NetHrs=" + netHrs
+ "&Sunday=" + sunHrs
+ "&Monday=" + monHrs
+ "&Tuesday=" + tueHrs
+ "&Wednesday=" + wedHrs
+ "&Thursday=" + thuHrs
+ "&Friday=" + friHrs
+ "&Saturday=" + satHrs
+ "&Fromdate=" + fromDate
+ "&Todate=" + toDate,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var systemHoliday = JSON.parse(data);
systemHoliday = systemHoliday.toFixed(2);
var time = systemHoliday.split('.');
eachMinutes = parseInt(time[0] * 60) + parseInt(time[1]);
netMinutes += eachMinutes
tr.find("#HolidayEnt").val(systemHoliday);
},
failure: function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log(response.responseText);
}
});
});
$("#totmins").val(netMinutes);
});
<input type="text" id="totmins" asp-for="TotalEntMins" class="form-control " data-role="text">
<table id="tblEntry" class="cell-border" style="width:100%">
<tbody>
<tr>
<td>@Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control datepicker w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.ToDate, new { htmlAttributes = new { @class = "form-control datepicker w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.SundayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.MondayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.TuesdayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.WednesdayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.ThursdayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.FridayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.SaturdayNetHrs, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.NetHrsPerWeek, new { htmlAttributes = new { type = "number", @class = "form-control w-100 empHrs" } })</td>
<td>@Html.EditorFor(model => model.HolidayEnt, new { htmlAttributes = new { type = "number", @class = "form-control w-100" } })</td>
</tr>
</tbody>
</table>
控制器方法
public decimal GetSystemCalculatedHolidayByDate(int EID, string NetHrs, string Sunday, string Monday, string Tuesday, string Wednesday,
string Thursday, string Friday, string Saturday, string Fromdate, string Todate)
{
decimal SystemHoliday = 0.00M;
if (EID > 0)
{
SystemHoliday = 210.30
}
return SystemHoliday;
}
【问题讨论】:
-
each 不是异步函数,所以是什么阻止您将其放入 each() { ... calculfunc(netMinutes) });
-
每个都不是异步的,它只是循环元素的简写