gpt4 book ai didi

jquery - 从文件加载 Javascript 对象

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

我在这个帖子中问了一个问题Stackoverflow ,并且效果完美。所以感谢所有给我回复的用户。但现在我有另一个问题。

我想将对象放在一个单独的文件中,所以我只需要更新该文件而不是JS文件(否则它会很大)。我正在使用 JQUERY。

我现在看起来像这样(所有信息都在 JS 文件中)。 IBANInfo 用于填充选择框

var IBANInfo = {
"ccAL":{
countryCode:"AL",
countryName:"Albanië",
IBANlength:"28",
bankFormCode:"0 8n 0 ",
accountNum:"0 16 0 "
},
"ccAD":{
countryCode:"AD",
countryName:"Andorra",
IBANlength:"24",
bankFormCode:"0 4n 4n",
accountNum:"0 12 0 "
},
"ccBE":{
countryCode:"BE",
countryName:"België",
IBANlength:"16",
bankFormCode:"0 3n 0 ",
accountNum:"0 7n 2n"
}
};
//---- then this function is used to build the selectList
function createSelect(){
var selectList = '';
var key;
selectList += "<option value=''>Kies een land</option>\n";
for (key in IBANInfo) {
if (IBANInfo.hasOwnProperty(key)) {
var countryInfo = IBANInfo[key];
selectList += "<option value='"+countryInfo.countryCode+"'>"+countryInfo.countryName+"</option>\n";
}
}
$('#selectBox').html(selectList);
}

我以为我可以这样做,但我在选择框中收到未定义的消息。

var IBANInfo = $.get('include/countryCodes.txt');

// also tried var IBANInfo = $.getJSON('include/countryCodes.txt');

//---- then this function is used to build the selectList
function createSelect(){
var selectList = '';
var key;
selectList += "<option value=''>Kies een land</option>\n";
for (key in IBANInfo) {
if (IBANInfo.hasOwnProperty(key)) {
var countryInfo = IBANInfo[key];
selectList += "<option value='"+countryInfo.countryCode+"'>"+countryInfo.countryName+"</option>\n";
}
}
$('#selectBox').html(selectList);
}

/*
the countryCodes.txt file is like this:
{
"ccAL":{
countryCode:"AL",
countryName:"Albani&euml;",
IBANlength:"28",
bankFormCode:"0 8n 0 ",
accountNum:"0 16 0 "
},
"ccAD":{
countryCode:"AD",
countryName:"Andorra",
IBANlength:"24",
bankFormCode:"0 4n 4n",
accountNum:"0 12 0 "
},
"ccBE":{
countryCode:"BE",
countryName:"Belgi&euml;",
IBANlength:"16",
bankFormCode:"0 3n 0 ",
accountNum:"0 7n 2n"
}
}
*/

我做错了什么。提前交易

额外信息:我已经创建了一个生成 IBAN 号码的网站,您可以在 iban.wswebcreation.nl 上找到它。在此站点上您可以生成 1-100 个 IBAN 号码。我想要做的是一种验证用户想要验证的 IBAN 号码的方法。他可以通过在输入字段中填写 IBAN 号码来做到这一点(请参阅其他网站)。

如果你查看源文件,你会看到 var IBANInfo 在 JS 文件中。我想把它放在一个单独的txt文件中加载。 IBANInfo 保存使用 IBAN 号码的国家/地区的所有信息(位数、国家/地区代码、帐号的构建方式等)。我现在在一些帮助下将其作为一个对象,请参阅前一个问题的链接。这有效。但将来我还想使用 IBANInfo 来验证 IBANnumber。

希望您现在有足够的信息

最佳答案

编辑:

要提供全局访问权限,您可以执行以下操作:

var IBANInfo = {};
$.getJSON('include/countryCodes.txt', function(json){ IBANInfo = $.extend({}, IBANInfo, data);});
<小时/>

您需要使用$.getJSON。此外,get 和 post 方法不会返回数据,因为它们需要异步工作,因此您实际上必须运行 createSelect 函数作为回调:

$.getJSON('include/countryCodes.txt', createSelect);

然后你 createSelect 需要至少接受一个参数,该参数将是数据......所以你的定义将如下所示:

function createSelect(IBANInfo){ /* your logic */}

关于jquery - 从文件加载 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2577393/

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