模板文字为空

时间:2019-02-16 15:03:39

标签: jquery ajax string literals

我在不同的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块。但是它为空。

1 个答案:

答案 0 :(得分:0)

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

$("#modalFlashContent").append(x);
$("#modalFlashHeader").text(y);
成功呼叫底部的

相关问题