template literal获取null

问题描述 投票:0回答:1

我在不同的jquery函数中有一些模板文字。其中一些正在发挥作用。但是这个块没有执行。

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

当AJAX请求发生时,这应该在x和y变量中给出一个HTML块。但它给出了null。

jquery ajax string literals
1个回答
0
投票

在函数的底部,x和y将为null,因为ajax调用的成功将比分配x和y时更快返回。你需要把

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

在成功通话的底部。

© www.soinside.com 2019 - 2024. All rights reserved.