I can't pass different values of variable through $.get() function please check this code to learn more about my problem
我无法通过$ .get()函数传递不同的变量值,请检查此代码以了解有关我的问题的更多信息
var addressFieldValues = ['address1', 'address2', 'address3'];
for(i=0; i<addressFieldValues.length; i++) {
var address = addressFieldValues[i];
$.get('function.php', address, function(data){
alert(address); // alerts address1 all time
});
}
Why is it alerting "address1" these 3 times? since it should alert 3 different addresses at all.
为什么这3次警告“地址1”?因为它应该警告3个不同的地址。
2 个解决方案
#1
2
One option would be to wrap your get request in a function, and pass in address as an argument. This way, you avoid the asynchronous issues.
一种选择是将get请求包装在一个函数中,并将地址作为参数传递。这样,您就可以避免异步问题。
function get(address) {
$.get('', address, function(data) {
alert(address);
});
}
var addressFieldValues = ['address1', 'address2', 'address3'];
for (i = 0; i < addressFieldValues.length; i++) {
var address = addressFieldValues[i];
get(address);
}
#2
0
Set request in object format, not string:
以对象格式设置请求,而不是字符串:
$.get('function.php', {'address': address}, function(data){
alert(data);
});
Second problem: alert server data instead.
第二个问题:改为警告服务器数据。
#1
2
One option would be to wrap your get request in a function, and pass in address as an argument. This way, you avoid the asynchronous issues.
一种选择是将get请求包装在一个函数中,并将地址作为参数传递。这样,您就可以避免异步问题。
function get(address) {
$.get('', address, function(data) {
alert(address);
});
}
var addressFieldValues = ['address1', 'address2', 'address3'];
for (i = 0; i < addressFieldValues.length; i++) {
var address = addressFieldValues[i];
get(address);
}
#2
0
Set request in object format, not string:
以对象格式设置请求,而不是字符串:
$.get('function.php', {'address': address}, function(data){
alert(data);
});
Second problem: alert server data instead.
第二个问题:改为警告服务器数据。