gpt4 book ai didi

javascript更新隐藏值

转载 作者:行者123 更新时间:2023-11-28 12:26:42 25 4
gpt4 key购买 nike

我有一个功能:

function add() {
$.ajax({
type: "POST",
url: "add.php",
async: "false", // Tried both- async: "false/true"
data: {
name: 'Test',
},
success: function(data) {
document.getElementById('id').value = data;
id = document.getElementById('id').value;
alert(id); // alerts here proper value
}
});

}

function testMyFunction() {
add();

// 'id' was set in add function.
id = document.getElementById('id').value;
alert(id); // Here does not alert value but blank(no value)
// This 'id' value is used at other place but here is issue.
}

调用 testMyFunction() 函数会出现上述问题。

可能出现什么问题?

最佳答案

$.ajax 是一个异步调用,它会在完成后更新“id”字段。您的代码在调用后(以及成功之前:调用 function(data) 之前)立即检查函数 testMyFunction() 中的值。

关于javascript更新隐藏值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com