PHP+JS 二階下拉選單問題

edited 十月 2013 in Javascript
假設
$city=1 (台北)
$zipcode=103 (大同區)
下拉選單台北是成功勾選
想請教如何勾選大同區?
<script LANGUAGE="javascript">

function Buildkey(num) {
	var ctr=1;
	document.CodeForm.subtype.selectedIndex=0;
	document.CodeForm.subtype.options[0]=new Option("請選擇區域...","");
	*/
	/*臺北市*/
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("中正區","100");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("大同區","103");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("中山區","104");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("松山區","105");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("大安區","106");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("萬華區","108");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("信義區","110");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("士林區","111");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("北投區","112");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("內湖區","114");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("南港區","115");	ctr=ctr+1;	}
	if(num=="1") {	document.CodeForm.subtype.options[ctr]=new Option("文山區","116");	ctr=ctr+1;	}
	/*基隆市*/
	if(num=="2") {	document.CodeForm.subtype.options[ctr]=new Option("仁愛區","200");	ctr=ctr+1;	}
	if(num=="2") {	document.CodeForm.subtype.options[ctr]=new Option("信義區","201");	ctr=ctr+1;	}
	if(num=="2") {	document.CodeForm.subtype.options[ctr]=new Option("中正區","202");	ctr=ctr+1;	}
	if(num=="2") {	document.CodeForm.subtype.options[ctr]=new Option("中山區","203");	ctr=ctr+1;	}
	if(num=="2") {	document.CodeForm.subtype.options[ctr]=new Option("安樂區","204");	ctr=ctr+1;	}
	if(num=="2") {	document.CodeForm.subtype.options[ctr]=new Option("暖暖區","205");	ctr=ctr+1;	}
	if(num=="2") {	document.CodeForm.subtype.options[ctr]=new Option("七堵區","206");	ctr=ctr+1;	}

	document.CodeForm.subtype.length=ctr;
	document.CodeForm.subtype.options[0].selected=true;
}
</script>


//縣市選單
<select name="City"  size=1 onChange="Buildkey(this.options[this.options.selectedIndex].value);">
<option value=0>請選擇縣市...</option>
<option value="1" <? if($city==1) echo "selected="selected"" ?>>臺北市</option>
<option value="2" <? if($city==2) echo "selected="selected"" ?>>基隆市</option>
</select>
//區域選單
<select name="subtype" size=1 onChange="document.CodeForm.value=this.options[this.options.selectedIndex].value;">
<option value="">請選擇區域..</option>
</select>	

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=7147

評論

  • edited 一月 2013
    1. 請愛用迴圈
    2. 能夠的話用 jquery 會比較簡潔
    3. 鄉鎮市區可以直接用郵遞區號當索引值,找起來會比較方便
    4. 在這一段程式碼的最後加上
    <script>
        Buildkey("1");
        for(k in document.CodeForm.subtype.options) {
            if(document.CodeForm.subtype.options[k].value == '103') {
                document.CodeForm.subtype.options[k].selected=true; 
            }
        }
    </script>
    
  • edited 一月 2013
    謝謝,可以讀到值了!!
Sign In or Register to comment.