javascript更新隐藏值
我有一个功能:javascript更新隐藏值,javascript,jquery,Javascript,Jquery,我有一个功能: function add() { $.ajax({ type: "POST", url: "add.php", async: "false", // Tried both- async: "false/true" data: { name: 'Test', }, success: function(data) { document
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”字段。您的代码在调用后(以及在调用成功之前:调用函数(数据)),立即在函数testMyFunction()中检查其值。$。ajax是一个异步调用,如果调用完成,它会更新“id”字段。您的代码在调用之后(以及在调用成功之前:函数(数据))立即在函数testMyFunction()中检查其值。JavaScript是一种异步语言。简而言之,这意味着您的代码应该永远不会阻塞:函数应该立即完成或稍后调用,即在某些输入/输出操作完成后(例如AJAX请求) 编辑:顺便说一句,您的代码不起作用,因为即使使用
async:false
函数,也会在事件循环中调用success
函数,因此即使在同步AJAX之后的代码也会发生这种情况。如果使用async:true
,AJAX将被阻塞,但在任何情况下,success
函数都将被异步调用。
因此,要同步处理数据,您不必使用success
函数,而必须使用$返回的对象。ajax()
调用:
var xhr = $.ajax({
type: "POST",
async: false,
url: "add.php",
data: {
name: 'Test',
},
});
alert(xhr.responseText); // Alerts result
因此,您应该永远不要使用async:false
。相反,最好将代码重构为如下所示:
function add(callback) {
$.ajax({
type: "POST",
url: "add.php",
data: {
name: 'Test',
},
success: callback
});
}
function testMyFunction() {
add(function(data) {
// This closure will be called AFTER the request is complete.
document.getElementById('id').value = data;
alert(data); // Alerts proper value.
});
}
action1(function(result1) {
action2(result1, function(result2) {
alert(action3(result2));
});
});
基本上,这个伪代码是错误的:
result1 = action1();
result2 = action2(result1);
result3 = action3(result2);
…应该这样写:
function add(callback) {
$.ajax({
type: "POST",
url: "add.php",
data: {
name: 'Test',
},
success: callback
});
}
function testMyFunction() {
add(function(data) {
// This closure will be called AFTER the request is complete.
document.getElementById('id').value = data;
alert(data); // Alerts proper value.
});
}
action1(function(result1) {
action2(result1, function(result2) {
alert(action3(result2));
});
});
JavaScript是一种异步语言。简而言之,这意味着您的代码应该永远不会阻塞:函数应该立即完成或稍后调用,即在某些输入/输出操作完成后(例如AJAX请求)
编辑:顺便说一句,您的代码不起作用,因为即使使用async:false
函数,也会在事件循环中调用success
函数,因此即使在同步AJAX之后的代码也会发生这种情况。如果使用async:true
,AJAX将被阻塞,但在任何情况下,success
函数都将被异步调用。
因此,要同步处理数据,您不必使用success
函数,而必须使用$返回的对象。ajax()
调用:
var xhr = $.ajax({
type: "POST",
async: false,
url: "add.php",
data: {
name: 'Test',
},
});
alert(xhr.responseText); // Alerts result
因此,您应该永远不要使用async:false
。相反,最好将代码重构为如下所示:
function add(callback) {
$.ajax({
type: "POST",
url: "add.php",
data: {
name: 'Test',
},
success: callback
});
}
function testMyFunction() {
add(function(data) {
// This closure will be called AFTER the request is complete.
document.getElementById('id').value = data;
alert(data); // Alerts proper value.
});
}
action1(function(result1) {
action2(result1, function(result2) {
alert(action3(result2));
});
});
基本上,这个伪代码是错误的:
result1 = action1();
result2 = action2(result1);
result3 = action3(result2);
…应该这样写:
function add(callback) {
$.ajax({
type: "POST",
url: "add.php",
data: {
name: 'Test',
},
success: callback
});
}
function testMyFunction() {
add(function(data) {
// This closure will be called AFTER the request is complete.
document.getElementById('id').value = data;
alert(data); // Alerts proper value.
});
}
action1(function(result1) {
action2(result1, function(result2) {
alert(action3(result2));
});
});
下一次请你把代码正确缩进好吗?!代码风格很糟糕。另外,您不希望
async:false
。只要在请求成功后使用回调异步调用您的代码。@ThiefMaster,告诉某人他们的帖子很糟糕真的是欢迎第一次询问者加入社区的最佳方式吗?是的,本来可以用更好的措辞。另一方面,一个人应该首先编写正确缩进的代码。在我的情况下,他也多次受到同样的欢迎。@LcSalazar,他确实这样做了,在我的评论之后进行了编辑。下次请你正确缩进代码好吗?!代码风格很糟糕。另外,您不希望async:false
。只要在请求成功后使用回调异步调用您的代码。@ThiefMaster,告诉某人他们的帖子很糟糕真的是欢迎第一次询问者加入社区的最佳方式吗?是的,本来可以用更好的措辞。另一方面,一个人应该首先编写正确缩进的代码。在我的情况下,他也曾多次受到过类似的欢迎。@LcSalazar,他确实这样做了,在我的评论之后进行了编辑。永远不要说永远。;)不管怎样,你知道为什么在OP的情况下同步调用它不起作用吗?啊,我明白了。抢手货永远不要说永远不管怎样,你知道为什么在OP的情况下同步调用它不起作用吗?啊,我明白了。抢手货