我正在使用http://www.javascriptsource.com/forms/country-state-city-drop-down-list.html
中包含的下拉列表// State lists
var states = new Array();
states['Canada'] = new Array('Alberta', 'British Columbia', 'Ontario');
states['Mexico'] = new Array('Baja California', 'Chihuahua', 'Jalisco');
states['United States'] = new Array('California', 'Florida', 'New York');
// City lists
var cities = new Array();
cities['Canada'] = new Array();
cities['Canada']['Alberta'] = new Array('Edmonton', 'Calgary');
cities['Canada']['British Columbia'] = new Array('Victoria', 'Vancouver');
cities['Canada']['Ontario'] = new Array('Toronto', 'Hamilton');
cities['Mexico'] = new Array();
cities['Mexico']['Baja California'] = new Array('Tijauna', 'Mexicali');
cities['Mexico']['Chihuahua'] = new Array('Ciudad Juárez', 'Chihuahua');
cities['Mexico']['Jalisco'] = new Array('Guadalajara', 'Chapala');
cities['United States'] = new Array();
cities['United States']['California'] = new Array('Los Angeles', 'San Francisco');
cities['United States']['Florida'] = new Array('Miami', 'Orlando');
cities['United States']['New York'] = new Array('Buffalo', 'new York');
function setStates() {
cntrySel = document.getElementById('country');
stateList = states[cntrySel.value];
changeSelect('state', stateList, stateList);
setCities();
}
function setCities() {
cntrySel = document.getElementById('country');
stateSel = document.getElementById('state');
cityList = cities[cntrySel.value][stateSel.value];
changeSelect('city', cityList, cityList);
}
function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i = 0; i < newOptions.length; i++) {
selectField.options[selectField.length] = newOption(newOptions[i], newValues[i]);
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
setStates();
});
Head < script type = "text/javascript"
src = "countryStateCity.js" > << / script > Body < fieldset style = "width: 230px;" > <legend><strong>Make your selection</strong></legend> < p > <form name="test" method="POST" action="processingpage.php">
<table>
<tr>
<td style="text-align: left;">Country:</td>
<td style="text-align: left;">
<select name="country" id="country" onchange="setStates();">
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="United States">United States</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">State:</td>
<td style="text-align: left;">
<select name="state" id="state" onchange="setCities();">
<option value="">Please select a Country</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">City:</td>
<td style="text-align: left;">
<select name="city" id="city">
<option value="">Please select a Country</option>
</select>
</td>
</tr>
</table>
</form> < /fieldset>
我已经让代码工作正常但只有很少的内容。当我加载所有国家/地区,州和地区,&amp;城市与城市城镇到countryStateCity.js文件我的电脑内存不足。
“countryStateCity.js”文件非常庞大。如果我列出所有国家和所有州和&amp;地区和城市&amp;以“A”开头的国家的城镇“B”一切正常,但如果我添加城市&amp;以“C”开头的国家的城镇系统失败。
我需要将每个国家/地区的源文件分解为1;
src="country/Canada.js"
src="country/Mexico.js"
src="country/United States.js"
我希望Javascript找到像country / Canada.js这样的文件名,而不是在整个世界文件中找到一个名字。
有人可以为我提供经过修改的“Javascript”编码吗?
答案 0 :(得分:0)
行。这很简单。我无法访问您的国家/地区文件,因此我不知道它是如何划分范围或分解,IE数组或JSON。所以我的代码只是你的一个混搭,它将无法正常工作。但它显示了如何通过快速的最终结果解决您的问题。
诀窍就是加载您需要的数据。通常这是通过API完成的,你通过ajax调用它告诉它你想要什么,并将它发送回来,这样你的UI就可以显示它。
在这种情况下,我们没有服务器,因此我们将通过动态将脚本加载到dom中来实现。在这种情况下,国家数据充满州和城市。请求时每个国家/地区一个文件。
最好是加载状态数据,然后加载每个州的城市等等。如果内存存在问题,还可以考虑是否应该从Dom中删除数据:
+------------+----------------------+----------------+
| mytable.id | mytable.a | mytable.b |
+------------+----------------------+----------------+
| 1 | ["P908","S57","A65"] | ["P908","S57"] |
| 2 | ["P908","S57","A65"] | ["P9","S5777"] |
+------------+----------------------+----------------+
&#13;