我如何在不耗尽内存的情况下将所有城市加载到下拉菜单中



我正在使用http://www.javascriptsource.com/forms/country-city-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;地区,&amp;城市&amp;进入countrystatecity.js的城镇文件我的计算机耗尽了内存。

" countrystatecity.js"文件很大。如果我列出了所有国家和所有州&amp;地区和城市&amp;国家的城镇以" a"&amp;" B"一切都可以,但是如果我添加城市&amp;从" C"开始的国家的城镇该系统失败。

我需要将源文件分解为每个国家/地区的1个;

src="country/Canada.js"
src="country/Mexico.js"
src="country/United States.js"

我希望JavaScript找到一个诸如country/canada.js之类的文件名,而不是在整个世界文件中找到名称。

有人可以为我提供修改的" JavaScript"编码?

好。这很简单。我无法访问您的国家文件,因此我不知道它是如何示范或分解的,即阵列或JSON。因此,我的代码只是您的捣碎,它将行不通。但这显示了如何通过用户体验快速最终结果解决问题。

诀窍只是在需要时加载所需的数据。通常这是通过API完成的,您可以通过Ajax拨打它告诉您您想要什么,并将其发送回去,以便您可以显示它。

在这种情况下,我们没有服务器,因此我们将通过将脚本加载到DOM中来做到这一点。在这种情况下,国家数据充满了各州和城市。要求时每个国家的一个文件。

最好加载状态数据,然后每个州的城市等等。以及如果内存是一个问题,以及是否应考虑是否应从DOM中删除数据:

//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;
//
// This will append scripts to the body so that you dont need to load all the 
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
  var s = document.createElement( 'script' );
  s.setAttribute( 'src', src );
  s.onload=callback;
  document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
	_COUNRTY = document.getElementById('country'),
	file = ["country/",_COUNTRY,".js"].join;
	addScript(file, setStates);	
};
//
// 
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state, 
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
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();
});
   

最新更新