如何在ajax回调后更新javascript变量
var x =1;
$(document).on('click','.classname',function(){
console.log(x);
});
$.ajax({
type:'post',
url:tmplUri+'/ajax/test.php',
cache:false,
async:false,
data:{journey:'yes'},
success: function(data)
{
x = data;
}
});
在ajax调用后,我需要更新该页面上的变量
请对此问题提出任何建议
答案 0 :(得分:2)
为此需要一个全局变量,只需调用另一个函数在ajax调用后重置变量
var x // make it globle
$(document).on('click','.classname',function(){
console.log(x);
});
function resetx(){
x =1;
}
$.ajax({
type:'post',
url:tmplUri+'/ajax/test.php',
cache:false,
async:false,
data:{journey:'yes'},
success: function(data)
{
x = data;
}
});
calling hierarchy
ajaxcall
resetx
than check you variable it will give you 1
这里是fiddle