gpt4 book ai didi

javascript - 为什么这个 JsFiddle 没有加载到我的页面上? (使用括号)

转载 作者:行者123 更新时间:2023-12-01 01:51:42 26 4
gpt4 key购买 nike

我正在使用括号。我第一次尝试实现一个脚本。所以我想用这个人的脚本: https://jsfiddle.net/patelriki13/m1ezs70o/

我将 Javascript 部分粘贴到 <head> 标记内

var countryStateInfo = {
"USA": {
"California": {
"Los Angeles": ["90001", "90002", "90003", "90004"],
"San Diego": ["92093", "92101"]
},
"Texas": {
"Dallas": ["75201", "75202"],
"Austin": ["73301", "73344"]
}
},
"India": {
"Assam": {
"Dispur": ["781005"],
"Guwahati" : ["781030", "781030"]
},
"Gujarat": {
"Vadodara" : ["390011", "390020"],
"Surat" : ["395006", "395002"]
}
}
}


window.onload = function () {

//Get html elements
var countySel = document.getElementById("countySel");
var stateSel = document.getElementById("stateSel");
var citySel = document.getElementById("citySel");
var zipSel = document.getElementById("zipSel");

//Load countries
for (var country in countryStateInfo) {
countySel.options[countySel.options.length] = new Option(country, country);
}

//County Changed
countySel.onchange = function () {

stateSel.length = 1; // remove all options bar first
citySel.length = 1; // remove all options bar first
zipSel.length = 1; // remove all options bar first

if (this.selectedIndex < 1)
return; // done

for (var state in countryStateInfo[this.value]) {
stateSel.options[stateSel.options.length] = new Option(state, state);
}
}

//State Changed
stateSel.onchange = function () {

citySel.length = 1; // remove all options bar first
zipSel.length = 1; // remove all options bar first

if (this.selectedIndex < 1)
return; // done

for (var city in countryStateInfo[countySel.value][this.value]) {
citySel.options[citySel.options.length] = new Option(city, city);
}
}

//City Changed
citySel.onchange = function () {
zipSel.length = 1; // remove all options bar first

if (this.selectedIndex < 1)
return; // done

var zips = countryStateInfo[countySel.value][stateSel.value][this.value];
for (var i = 0; i < zips.length; i++) {
zipSel.options[zipSel.options.length] = new Option(zips[i], zips[i]);
}
}
}

我将 HTML 部分粘贴到 <body> 标记内

<form name="myform" id="myForm">
<select id="countySel" size="1">
<option value="" selected="selected">-- Select Country --</option>
</select>
<br>
<br>

<select id="stateSel" size="1">
<option value="" selected="selected">-- Select State--</option>
</select>
<br>
<br>
<select id="citySel" size="1">
<option value="" selected="selected">-- Select City--</option>
</select>
<br>
<br>
<select id="zipSel" size="1">
<option value="" selected="selected">-- Select Zip--</option>
</select>
</form>

然而,当页面加载时,当我单击下拉框时,Javascript 代码中的任何选项都不会显示。

我这里缺少一些额外的步骤吗?第一次尝试使用脚本。

最佳答案

通过放置 <script> 解决了问题在 body 的末端。似乎它干扰了其他可能的负载。

var countryStateInfo = {
"USA": {
"California": {
"Los Angeles": ["90001", "90002", "90003", "90004"],
"San Diego": ["92093", "92101"]
},
"Texas": {
"Dallas": ["75201", "75202"],
"Austin": ["73301", "73344"]
}
},
"India": {
"Assam": {
"Dispur": ["781005"],
"Guwahati" : ["781030", "781030"]
},
"Gujarat": {
"Vadodara" : ["390011", "390020"],
"Surat" : ["395006", "395002"]
}
}
}


window.onload = function () {

//Get html elements
var countySel = document.getElementById("countySel");
var stateSel = document.getElementById("stateSel");
var citySel = document.getElementById("citySel");
var zipSel = document.getElementById("zipSel");

//Load countries
for (var country in countryStateInfo) {
countySel.options[countySel.options.length] = new Option(country, country);
}

//County Changed
countySel.onchange = function () {

stateSel.length = 1; // remove all options bar first
citySel.length = 1; // remove all options bar first
zipSel.length = 1; // remove all options bar first

if (this.selectedIndex < 1)
return; // done

for (var state in countryStateInfo[this.value]) {
stateSel.options[stateSel.options.length] = new Option(state, state);
}
}

//State Changed
stateSel.onchange = function () {

citySel.length = 1; // remove all options bar first
zipSel.length = 1; // remove all options bar first

if (this.selectedIndex < 1)
return; // done

for (var city in countryStateInfo[countySel.value][this.value]) {
citySel.options[citySel.options.length] = new Option(city, city);
}
}

//City Changed
citySel.onchange = function () {
zipSel.length = 1; // remove all options bar first

if (this.selectedIndex < 1)
return; // done

var zips = countryStateInfo[countySel.value][stateSel.value][this.value];
for (var i = 0; i < zips.length; i++) {
zipSel.options[zipSel.options.length] = new Option(zips[i], zips[i]);
}
}
}
<form name="myform" id="myForm">
<select id="countySel" size="1">
<option value="" selected="selected">-- Select Country --</option>
</select>
<br>
<br>

<select id="stateSel" size="1">
<option value="" selected="selected">-- Select State--</option>
</select>
<br>
<br>
<select id="citySel" size="1">
<option value="" selected="selected">-- Select City--</option>
</select>
<br>
<br>
<select id="zipSel" size="1">
<option value="" selected="selected">-- Select Zip--</option>
</select>
</form>

<p>First select a country => state => city => zipcode</p>

关于javascript - 为什么这个 JsFiddle 没有加载到我的页面上? (使用括号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51448466/

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