gpt4 book ai didi

javascript - JS 全局变量传递为未定义 - jquery mobile

转载 作者:行者123 更新时间:2023-12-03 11:44:22 26 4
gpt4 key购买 nike

我正在尝试连接 2 个 js 文件,但我似乎无法将变量传递给它。

语言.js 文件:

//SELECT LANGUAGE
var login_user, login_pass, login_page_loading_msg, login_button, login_register;
var dash_log_out, dash_loading_msg, dash_open_lifetime, dash_remind, dash_nothanks, dash_logout, dash_revenue, dash_value, dash_campaign, dash_status, dash_layer, dash_popdown, dash_leadorsale;
var stat_date, stat_view, stat_clicks;
var prog_choose, prog_info, prog_payperorder, prog_ordervolume, prog_countries, prog_conditions, prog_share, prog_url, prog_copytoclipboard, prog_copiedtoclipboard, prog_success, prog_ok;
var reg_title, reg_earn, reg_startearning, reg_participfree, reg_generalinfo, reg_language, reg_prefuser, reg_email, reg_details, reg_usertitle, reg_fn, reg_ln, reg_street, reg_postcode, reg_country, reg_tandc, reg_submit;
var menu_dash, menu_stat, menu_prog, menu_news;

$.getJSON("data/res/en.json", function(json) {

//ATTRIBUTE LANGUAGE
//LOGIN PAGE VARIABLES

login_user = json['login']['user'];
login_pass = json['login']['pass'];
login_page_loading_msg = json['login']['page_loading_msg'];
login_register = json['login']['register'];
login_button = json['login']['login'];

//DASHBOARD PAGE VARIABLES

dash_log_out = json['dashboard']['log_out'];
dash_loading_msg = json['dashboard']['loading_msg'];
dash_open_lifetime = json['dashboard']['open_lifetime'];
dash_remind = json['dashboard']['remind'];
dash_nothanks = json['dashboard']['nothanks'];
dash_logout = json['dashboard']['logout'];
dash_revenue = json['dashboard']['revenue'];
dash_value = json['dashboard']['value'];
dash_campaign = json['dashboard']['campaign'];
dash_status = json['dashboard']['status'];
dash_adklicks = json['dashboard']['adklicks'];
dash_layer = json['dashboard']['layer'];
dash_popdown = json['dashboard']['popdown'];
dash_leadorsale = json['dashboard']['leadorsale'];

$('#logOut').html(dash_log_out);
$('.loading').html(dash_loading_msg);


//STATISTICS PAGE VARIABLES

stat_date = json['statistics']['date'];
stat_view = json['statistics']['views'];
stat_clicks = json['statistics']['clicks'];

//PROGRAMS PAGE VARIABLES

prog_choose = json['program']['choose'];
prog_info = json['program']['info'];
prog_payperorder = json['program']['payperorder'];
prog_ordervolume = json['program']['ordervolume'];
prog_countries = json['program']['countries'];
prog_conditions = json['program']['conditions'];
prog_share = json['program']['share'];
prog_url = json['program']['url'];
prog_copytoclipboard = json['program']['copytoclipboard'];
prog_copiedtoclipboard = json['program']['copiedtoclipboard'];
prog_success = json['program']['success'];
prog_ok = json['program']['ok'];

//REGISTRATION PAGE VARIABLES


reg_title = json['registration']['title'];
reg_earn = json['registration']['earn'];
reg_startearning = json['registration']['startearning'];
reg_participfree = json['registration']['participfree'];
reg_generalinfo = json['registration']['generalinfo'];
reg_language = json['registration']['language'];
reg_prefuser = json['registration']['prefuser'];
reg_email = json['registration']['email'];
reg_details = json['registration']['details'];
reg_usertitle = json['registration']['usertitle'];
reg_fn = json['registration']['fn'];
reg_ln = json['registration']['ln'];
reg_street = json['registration']['street'];
reg_postcode = json['registration']['postcode'];
reg_country = json['registration']['country'];
reg_tandc = json['registration']['tandc'];
reg_submit = json['registration']['submit'];

//MENU VARIABLES


menu_dash = json['menu']['dash'];
menu_stat = json['menu']['stat'];
menu_prog = json['menu']['prog'];
menu_news = json['menu']['news'];
});

main.js 文件:

 [...]

console.log('login_button:'+ login_button);

[...]

这给了我:login_button:undefined

如何从第一个文件的函数内传递变量值?如果我将它分配在获取 json 值的函数外部,那么它会正确传递,但函数内部的任何内容都会传递为未定义。

**

我是如何解决的:

**

语言.js:

jQuery.extend({
getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
result = data;
}
});
return result;
}
});
var results = $.getValues(yourUrlString);

最佳答案

您可以在 main.js 中定义一个 init 函数,在 language.js 之前加载 main.js。在 language.js 中,您在完成变量设置后调用 init 函数。我还建议您为全局变量命名。将它们全部放在一个巨大的物体中,也许还有一个独特的名字。这可以避免将太多全局变量放入全局对象中并使其难以跟踪。

关于javascript - JS 全局变量传递为未定义 - jquery mobile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26117191/

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