我是編碼新手,因此我沒有完全理解這段代碼。
我想要實現的是我想放在多個下拉菜單。
我的代碼的問題是,它只接受來自某些下拉菜單的值。使用不同下拉菜單中的值
我的代碼可以幫助用戶根據他們在上一個下拉菜單中選擇的內容選擇地區,國家和州。我使用這個代碼是因爲它與我計劃實現的類似。
我面對的問題是我有大約8個需要使用數據的下拉菜單。有8個地區下降,8個國家下降,8個國家下降。全部在同一頁面。如果我放8個代碼,代碼將停止工作。我究竟做錯了什麼?
有人可以幫我嗎?這是代碼:
<form>
Region» <select onchange="set_country(this,country,city_state)" size="1" name="region">
<option value="" selected="selected">SELECT NOTE</option>
<option value=""></option>
<script type="text/javascript">
setRegions(this);
</script>
</select>
Country» <select name="country" size="1" disabled="disabled" onchange="set_city_state(this,city_state)"></select>
City/State» <select name="city_state" size="1" disabled="disabled" onchange="print_city_state(country,this)"></select>
<script>
////////////////////////////////////////////////////////////////////////////
// city_state.js ///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
var countries = Object();
countries['C'] = '|C|Cm|';
countries['D'] = '|D|Dm|';
////////////////////////////////////////////////////////////////////////////
var city_states = Object();
//C
city_states['C'] = '|032010|335553|';
city_states['Cm'] = '|335543|';
city_states['D'] = '|000232|557775';
city_states['Dm'] = '|000231|557765';
////////////////// ////////////////// /////////// ////////////////////////////////////////////////// ///////////////
function setRegions()
{
for (region in countries)
document.write('<option value="' + region + '">' + region + '</option>');
}
function set_country(oRegionSel, oCountrySel, oCity_StateSel)
{
var countryArr;
oCountrySel.length = 0;
oCity_StateSel.length = 0;
var region = oRegionSel.options[oRegionSel.selectedIndex].text;
if (countries[region])
{
oCountrySel.disabled = false;
oCity_StateSel.disabled = true;
oCountrySel.options[0] = new Option('SELECT TYPE','');
countryArr = countries[region].split('|');
for (var i = 0; i < countryArr.length; i++)
oCountrySel.options[i + 1] = new Option(countryArr[i], countryArr[i]);
document.getElementById('txtregion').innerHTML = region;
document.getElementById('txtplacename').innerHTML = '';
}
else oCountrySel.disabled = true;
}
function set_city_state(oCountrySel, oCity_StateSel)
{
var city_stateArr;
oCity_StateSel.length = 0;
var country = oCountrySel.options[oCountrySel.selectedIndex].text;
if (city_states[country])
{
oCity_StateSel.disabled = false;
oCity_StateSel.options[0] = new Option('SELECT TAB','');
city_stateArr = city_states[country].split('|');
for (var i = 0; i < city_stateArr.length; i++)
oCity_StateSel.options[i+1] = new Option(city_stateArr[i],city_stateArr[i]);
document.getElementById('txtplacename').innerHTML = country;
}
else oCity_StateSel.disabled = true;
}
function print_city_state(oCountrySel, oCity_StateSel)
{
var country = oCountrySel.options[oCountrySel.selectedIndex].text;
var city_state = oCity_StateSel.options[oCity_StateSel.selectedIndex].text;
if (city_state && city_states[country].indexOf(city_state) != -1)
document.getElementById('txtplacename').innerHTML = city_state + ', ' + country;
else document.getElementById('txtplacename').innerHTML = country;
}
</script>
請看看在http://jsfiddle.net/HzJ9J/1/ 更新的代碼我沒有粘貼一個javascript代碼在JavaScript部分,因爲我認爲,特定的代碼需要在正確的地方運行。不確定是否必須將其移至javascript部分。
嗨鮑勃,我已經嘗試過,因爲你提到過。我可以添加另外3個下拉菜單,但只有第一個下拉菜單正在工作。其餘的人不工作。我究竟做錯了什麼?我真的不知道如何在Chrome中使用開發人員工具:( – Sudhin
由於您的代碼現在與您在問題中發佈的內容現在有所不同,因此我不知道您可能會將當前代碼放入jsfiddle.net並編輯你的問題提供一個指針。 如果你打算在JavaScript來開發,你將要學習的開發工具。 –
鮑勃嗨,我已經把代碼jsfiddle.net。能否請您檢查它告訴我的錯誤?非常感謝! – Sudhin