You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击View按钮后文本框无法加载学生详情问题排查

点击View按钮后学生信息无法填充到文本框的问题解决

核心问题:异步请求未正确等待响应

你的GetStudentById方法是异步调用API,但函数直接返回初始的空字符串obj,此时API请求还未完成,所以FillStudentInfo中拿到的studInfo是空值,导致文本框无法被赋值。

解决方案:重构异步方法,确保数据返回后再执行填充

1. 修改StudentSummaryManager中的异步方法为Promise风格

var StudentSummaryManager = {
    GetAllStudents: function () {
        return new Promise(function(resolve, reject) {
            var serviceUrl = "https://localhost:5001/api/student";
            AjaxManager.GetAPI(serviceUrl, resolve, reject);
        });
    },
    GetStudentById: function (studentId) {
        return new Promise(function(resolve, reject) {
            var serviceUrl = "https://localhost:5001/api/student/" + studentId;
            AjaxManager.GetAPI(serviceUrl, resolve, reject);
        });
    }
};

2. 修改FillStudentInfo方法,使用async/await等待数据返回

同时修正checkbox的赋值逻辑(checkbox不能用val(),需用prop('checked')),以及日期格式适配input的date类型:

FillStudentInfo: async function (studentId) {
    debugger;
    var studInfo = await StudentSummaryManager.GetStudentById(studentId);

    $("#btnSave").text("Update");
    $("#divDetails").show();
    $("#divSummary").hide();

    $("#txtStudentFirstName").val(studInfo.FirstName);
    $("#txtStudentLastName").val(studInfo.LastName);
    // 将API返回的日期转为input date支持的YYYY-MM-DD格式
    $("#txtStudentDate").val(new Date(studInfo.DateofEnrollment).toISOString().split('T')[0]);
    // 根据API返回的Active值设置checkbox状态
    $("#txtStudentActive").prop('checked', studInfo.Active === true || studInfo.Active === "true");
}

3. 修复表格加载的异步问题

原LoadStudent方法同样存在异步问题,导致表格可能无法渲染,同步修改为:

LoadStudent: async function () {
    $("#Table tbody tr").remove();
    var studentList = await StudentSummaryManager.GetAllStudents();
    $.each(studentList, function (i, item) {
        var rows = "<tr>" +
            "<td>" + item.StudentId + "</td>" +
            "<td>" + item.FirstName + "</td>" +
            "<td>" + item.LastName + "</td>" +
            "<td>" + new Date(item.DateofEnrollment).toLocaleDateString() + "</td>" +
            "<td>" + (item.Active ? "Yes" : "No") + "</td>" +
            "<td><button class='btn btn-info' onClick='StudentSummaryHelper.FillStudentInfo(" + item.StudentId + ")'>View</button></td>" +
            "</tr>";
        $("#Table tbody").append(rows);
    });
}

4. 修复其他潜在bug

  • StudentDetailsHelper.InitStudentDetails中$("btnSave")缺少ID选择符#,应改为$("#btnSave")
  • StudentDetailsHelper.ClearForms中$("txtStudentActive")缺少ID选择符#,且checkbox清空需用prop('checked', false):
ClearForms() {
    $("#txtStudentFirstName").val("");
    $("#txtStudentLastName").val("");
    $("#txtStudentDate").val("");
    $("#txtStudentActive").prop('checked', false);
}

内容的提问来源于stack exchange,提问作者Jarryd vn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 16:27:37