1 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
好的,所以我對(duì) $.ajax 調(diào)用使用了 async/await,然后在更改事件處理程序中,我使用 .then 方法對(duì)生成的數(shù)據(jù)進(jìn)行操作。(也可以在事件處理程序中使用異步等待,但是由于您最初擁有它并且它不起作用,因此我選擇了promise)。
我很確定這應(yīng)該有效,但如果沒有,請(qǐng)讓我知道控制臺(tái)顯示的內(nèi)容。
注意您可能需要在設(shè)置每個(gè)選擇器的值之前,控制臺(tái).log結(jié)果并提取要查找的數(shù)據(jù)。您可以在 .then 方法中執(zhí)行此操作。
async function executeAjax(url) {
let result;
try {
result = await $.ajax({
url: url,
type: "GET"
});
return result;
} catch (error) {
console.log(error);
}
}
$('#selector1').change(function(e) {
executeAjax('api/selector2' + $("#selector1").val())
.then((result) => {
// console.log(result); <-- may need to find and pull the data you are looking for out of result
// let myVal = result[?]['something'].orother;
$("#selector2").val(result);
});
executeAjax('api/selector3' + $("#selector1").val())
.then((result) => {
// console.log(result); <-- may need to find and pull the data you are looking for out of result
// let myVal = result[?]['something'].orother;
$("#selector3").val(result);
});
});
添加回答
舉報(bào)