gpt4 book ai didi

jquery - 无法访问 jQuery 中的变量值

转载 作者:行者123 更新时间:2023-12-01 08:14:22 25 4
gpt4 key购买 nike

我遇到的问题是我无法访问函数外部变量的值。我刚刚开始学习 jQuery,我真的很喜欢它,但这已经让我卡住了很长一段时间了。

我已经阅读了与此相关的其他线程,但这些方法仍然不起作用!

    $.get("/new/verify.php", { username: username, email: email },
function(data){
var stop_loading = '0';
if(data == 'username')
{
alert('username taken');
$('#username_taken_error').show();
$('#username').focus();
stop_loading = '1';
}else if(data == 'email') {
alert('email taken');
$('#email_taken_error').show();
$('#email').focus();
stop_loading = '1';
}else if(data == 'username|email') {
alert('username/email taken');
$('#username_taken_error').show();
$('#email_taken_error').show();
$('#username').focus();
stop_loading = '1';
}
});

alert(stop_loading);

if(stop_loading == '1') {
alert('Stop loading');
return false;
}

最佳答案

由于 $.get() 执行异步 AJAX 请求,因此在 $.get() success 函数中使用 stop_loading 调用另一个函数如下:

 $.get("/new/verify.php", { username: username, email: email },
function(data){
var stop_loading = '0';
if(data == 'username')
{
alert('username taken');
$('#username_taken_error').show();
$('#username').focus();
stop_loading = '1';
}else if(data == 'email') {
alert('email taken');
$('#email_taken_error').show();
$('#email').focus();
stop_loading = '1';
}else if(data == 'username|email') {
alert('username/email taken');
$('#username_taken_error').show();
$('#email_taken_error').show();
$('#username').focus();
stop_loading = '1';
}
// call function with stop_loading
callAFunction(stop_loading);
});

// this function will be called
// with stop_loading arugment

function callAFunction(stop_loading){
if(stop_loading == '1') {
alert('Stop loading');
return false;
}
}

关于jquery - 无法访问 jQuery 中的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11699465/

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