1 回答

TA貢獻1863條經驗 獲得超2個贊
好的,所以我對 $.ajax 調用使用了 async/await,然后在更改事件處理程序中,我使用 .then 方法對生成的數據進行操作。(也可以在事件處理程序中使用異步等待,但是由于您最初擁有它并且它不起作用,因此我選擇了promise)。
我很確定這應該有效,但如果沒有,請讓我知道控制臺顯示的內容。
注意您可能需要在設置每個選擇器的值之前,控制臺.log結果并提取要查找的數據。您可以在 .then 方法中執行此操作。
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);
});
});
添加回答
舉報