游.程 2019-02-16 15:03 采纳率: 0%
浏览 7

模板文字为空

I have some template literals in different jquery functions. some of them are working. But this block is not executing.

function viewUserDetails(data) { // gets integer {1,2,3,...}
    let x = ``; // ""
    let y = ``; // ""
    $.ajax({
        type : "GET",
        url : 'http://localhost/bugs_javascript/api/public/getSingleUser.php',
        data : {
            "id" : data // gets value {1,2,3,...}
        },
        dataType : 'json',
        contentType : 'application/json',
        success: function (e) {
            console.log("success: "+e); // logs the returned JSON object
            x += `<label for="title">Full Name</label>
                    <input type="text" class="form-control" 
id="updateUserName" oninput="checkName('update')"
                            value="${e.name}">
                    <span id="uNameMsg"></span>
                    <br>
                    <label for="title">Unique ID</label>
                    <input type="textarea" class="form-control" id="updateUserUnique" rows="5"
                            value="${e.nick_name}" 
oninput="checkUnique('update', ${e.nick_name})">
                    <span id="uUniqueMsg"></span>
                    <br>
                    <label for="title">Email</label>
                    <input type="text" class="form-control" 
id="updateUserEmail"
                            value="${e.email}" 
oninput="checkEmail('update', ${e.email})">
                    <span id="uEmailMsg"></span>
                    <br>
                    <button class="btn btn-success" 
id="editUserFButtton"
                            onclick="updateUser(${e.id})">Update 
Record</button>
                    <span id="updateUserBtnMsg"></span>`; // x = ""
            y = `User Registered on: ${e.registration_date}`; // y = ""
        },
        error : function (e) {
            console.log("error: "+e);
        }
    });

    $("#modalFlashContent").append(x); // "" null
    $("#modalFlashHeader").text(y); // "" null
} // Not working. Some prooblem

This should give an HTML block in x and y variable as the AJAX request is happening. But it gives null.

  • 写回答

1条回答 默认 最新

  • 7*4 2019-02-16 21:20
    关注

    At the bottom of the function, x and y are going to be null because the success of your ajax call is going to return later than when you assign x and y. You need to put

    $("#modalFlashContent").append(x);
    $("#modalFlashHeader").text(y);
    

    at the bottom of the success call.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂