7-3 �ʺA�U�Ԧ����

¨Ï¥Î select ¼ÐÅÒ¡A§Ú­Ì¥i¥H²£¥Í³æ¿ï©Î¬O¦h¿ïªº¤U©Ô¦¡¿ï³æ¡A¦ý¬O³o¨Ç¿ï³æªº¿ï¶µ³£¬O©T©wªº¡A­Y­n¯à°÷§Y®É§ïÅܳo¨Ç¿ï¶µ¡A´N­n¾a JavaScript¡C¥»¸`±N»¡©ú¦p¦ó¥H JavaScript ¨Ó§Y®É§ïÅܳo¨Ç¿ï¶µ¡A¥H²£¥Í°ÊºAªº¤U©Ô¦¡¿ï³æ¡C

°²³]¤@­Ó¿ï³æªº¦WºÙ¬O theList¡A¨º»ò¥¦©Ò¨ã¦³ªº©Ê½è¥i¥H»¡©ú¦p¤U¡G

¦pªG§Ú­Ì­n§R°£¿ï³æªº¿ï¶µ¡A¦³¨âºØ§@ªk¡G
  1. ¨Ï¥Î theList.options[i]=null ¥i¥Hª½±µ§R°£²Ä i ­Ó¿ï¶µ¡C
  2. ¨Ï¥Î theList.options.length=n ¥i¥H±N¿ï¶µ­Ó¼Æ³]©w¬° n¡A¨ä¾l¦hªº¿ï¶µ±N·|³Q§R°£¡C
­Y­n¼W¥[¿ï¶µ¡A¥i¥H¨Ï¥Î¤U¦C©R¥O¡G theList.options[i]=new Option(text, value); ¨ä¤¤ text ©M value ¤À§O¥Nªí·s¿ï¶µªº¤å¦r©M­È¡C¥²¶·¤p¤ßªº¬O¡G ¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì¥i¥H°ÊºA¦a¼W¥[©Î§R°£¿ï¶µ¡G

Example¡]dynamicListBox01.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]dynamicListBox01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>°ÊºA¤U©Ô¦¡¿ï³æ¡G¼W¥[¿ï¶µ»P§R°£¿ï¶µ</h2>
<hr>

<script>
function deleteOption(list){
	var index=list.selectedIndex;
	if (index>=0)
		list.options[index]=null;
	else
		alert("µL¤Ï¥Õ¿ï¶µ¡I");
}

function addOption(list, text, value){
	var index=list.options.length;
	list.options[index]=new Option(text, value);
	list.selectedIndex=index;
}
</script>

<form>
<select id=theList size=5>
	<option value=¬P´Á¤@>Monday
	<option value=¬P´Á¤T>Wednesday
	<option value=¬P´Á¤­>Friday
	<option value=¬P´Á¤é>Sunday
</select>
<p>
<input type="button" value="§R°£¤Ï¥Õ¿ï¶µ" onclick="deleteOption(theList)"><br>
<input type="button" value="¼W¥[¥k¦C¿ï¶µ" onclick="addOption(theList, theText.value, theValue.value)">
Text: <input id=theText value="test">
Value: <input id=theValue value="test">
</form>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î id ¦Ó¤£¨Ï¥Î name ¨Ó¥Nªíªí³æ±±¨î¶µ¡A¨ä¦n³B¬O¥i¥Hª½±µ¨Ï¥Î id ¨Ó¨Ï¥Îªí³æ¤¸¯À¡A¦Ó¤£¥²¤@©w­n¸g¥Ñ¤å¥óª«¥ó¼Ò«¬ªº¶¥¼hµ²ºc¨Ó¥Ñ¤W¨ì¤U¡B¤@¼h¤@¼h«ü©w¡C

§Q¥Î¦P¼Ëªº·§©À¡A§Ú­Ì¥i¥H²£¥ÍÃþ¦ü¾ðª¬ªºµ²ºc¡A¥iÅý¨Ï¥ÎªÌ¶i¦æ¡u¥Ø¿ý·j´M¡v¡]Directory Search¡^¡C¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì§Q¥Î°ÊºA¤U©Ô¦¡¿ï³æ²£¥Í¨â¼hªº¾ðª¬µ²ºc¡A©ÎºÙ¡u¨â®Ø³s°Ê¡v¡A´«¥y¸Ü»¡¡A§A¥u­n¦b¥ª¤è¿ï¨ú¨t§O¡A¥k¤è´N·|¥X²{¦¨­û¡A½Ð¸Õ¬Ý¬Ý¡G

Example¡]dynamicListBox02.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]dynamicListBox02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>°ÊºA¤U©Ô¦¡¿ï³æ¡G¨â®Ø³s°Ê¡]¨â¼h¾ðª¬¿ï¶µ¡^</h2>
<hr>

<script>
department=new Array();
department[0]=["±i¶©¯¾", "¶À¯à´I", "¤ý¬±Â×", "±i¥@ªN", "±i´¼¬P"];	// ¸ê°T¨t
department[1]=["¶À·ç¬P", "¶À¥ò³®", "§f©¾¬z", "¾G³Õ®õ", "¿c¦V¦¨"];	// ¹q¾÷¨t
department[2]=["·¨·q°ó", "¤ý°ö¤¯", "¸­»ÊÅv", "§ºÂí°ê"];			// °Ê¾÷¨t
department[3]=["¤ý¤Ñ¤à", "¶}°õ¤¤", "±ç¥¿§»"];				// ¤u¬ì¨t

function renew(index){
	for(var i=0;i<department[index].length;i++)
		document.myForm.member.options[i]=new Option(department[index][i], department[index][i]);	// ³]©w·s¿ï¶µ
	document.myForm.member.length=department[index].length;	// §R°£¦h¾lªº¿ï¶µ
}
</script>

<form name="myForm">
¨t§O¡G
<select size=5 onChange="renew(this.selectedIndex);">
	<option value="¸ê°T¨t">¸ê°T¨t
	<option value="¹q¾÷¨t">¹q¾÷¨t
	<option value="°Ê¾÷¨t">°Ê¾÷¨t
	<option value="¤u¬ì¨t">¤u¬ì¨t
</select>

¶¤­û¡G
<select name="member" size=5>
	<option value="">½Ð¥Ñ¥ª¤è¿ï¨ú¨t§O
</select>
</form>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¥Î¨ì¤F¤Gºû°}¦C¡A¥H¤è«K¦s¨ú¬ÛÃö¸ê®Æ¡C

¨Ï¥ÎÃþ¦üªº¤è¦¡¡A§Ú­Ì¤]¥i¥H²£¥Í¤T¼hªº¾ðª¬µ²ºc¡A©ÎºÙ¡u¤T®Ø³s°Ê¡v¡C¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì±Nºq¦±¸ê®Æ¤À¦¨¤T­Ó¼h¦¸¡A¤À§O¬O»y¨¥¡Bºq¤â¡Bºq¦W¡A¥H«K¶i¦æ¥Ø¿ý¦¡·j´M¡A½d¨Ò¦p¤U¡G

Example¡]dynamicListBox03.htm¡^¡G

¦b¤W­z½d¨Ò¤¤¡A§A¥i¥H¥ý¦b¥ª¤èÂI¿ï»y¨¥¡A¤¤¶¡´N·|Åã¥Ü¬ÛÃöºq¤â¡A±µµÛÂI¿ïºq¤â¡A¥k¤è´N·|Åã¥Ü¬ÛÃöºq¦W¡A±µµÛ§A¦AÂI¿ïºq¦W¡A¤U¤è´N·|¥X²{§¹¾ãªº·j´M¸ô®|¡C­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]dynamicListBox03.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>°ÊºA¤U©Ô¦¡¿ï³æ¡G¤T®Ø³s°Ê¡]¤T¼h¾ðª¬¿ï¶µ¡^</h2>
<hr>

<script>
function node(name, child){
	this.name=name;
	this.child=child;
}

function dataHierarchy(){
	// °ê»yºq¤â
	var mandarin=new Array();
	var i=0;
	mandarin[i++]=new node("±i¾Ç¤Í", ["§Úµ¥¨ìªá¨à¤]Á¤F", "¤@¤d­Ó¶Ë¤ßªº²z¥Ñ", "©@°Ø"]);
	mandarin[i++]=new node("±i´f©f", ["Å¥®ü", "°Å·R", "­ì¨Ó§A¤°»ò³£¤£­n", "¤T¤Ñ¤T©]"]);
	mandarin[i++]=new node("¼B¼wµØ", ["ÁÂÁ§Aªº·R", "¨Ó¥Í½t", "§Ñ±¡¤ô"]);
	mandarin[i++]=new node(  "¥î¨Õ", ["®ö¤H±¡ºq", "¾ðªK©t³¾"]);
	mandarin[i++]=new node("©PµØ°·", ["ªá¤ß", "¤ßªº¤è¦V"]);
	// ­^»yºq¤â
	var english=new Array();
	var i=0;
	english[i++]=new node("Michael", ["Beat It", "Billie Jean", "Heal The World"]);
	english[i++]=new node("Celine", ["My Heart Will Go On", "Hope"]);
	// »y¨¥Ãþ§O
	var output=new Array();
	var i=0;
	output[i++]=new node("°ê»y", mandarin);
	output[i++]=new node("­^»y", english);

	return(output);
}
dataTree=dataHierarchy();

// ²Ä¤T­ÓÄæ¦ì³Q§ó°Ê«áªº¤ÏÀ³°Ê§@
function onChangeColumn3(){
	updatePath();
}

// ²Ä¤G­ÓÄæ¦ì³Q§ó°Ê«áªº¤ÏÀ³°Ê§@
function onChangeColumn2(){
	form=document.theForm;
	index1=form.column1.selectedIndex;
	index2=form.column2.selectedIndex;
	index3=form.column3.selectedIndex;
	// Create options for column 3
	for (i=0;i<dataTree[index1].child[index2].child.length;i++)
		form.column3.options[i]=new Option(dataTree[index1].child[index2].child[i], dataTree[index1].child[index2].child[i]);
	form.column3.options.length=dataTree[index1].child[index2].child.length;
	updatePath();
}

// ²Ä¤@­ÓÄæ¦ì³Q§ó°Ê«áªº¤ÏÀ³°Ê§@
function onChangeColumn1() {
	form=document.theForm;
	index1=form.column1.selectedIndex;
	index2=form.column2.selectedIndex;
	index3=form.column3.selectedIndex;
	// Create options for column 2
	for (i=0;i<dataTree[index1].child.length;i++)
		form.column2.options[i]=new Option(dataTree[index1].child[i].name, dataTree[index1].child[i].name);
	form.column2.options.length=dataTree[index1].child.length;
	// Clear column 3
	form.column3.options.length=0;
	updatePath();
}

// ­×§ï©ÒÅã¥Üªº¸ô®|
function updatePath(){
	form=document.theForm;
	index1=form.column1.selectedIndex;
	index2=form.column2.selectedIndex;
	index3=form.column3.selectedIndex;
	if ((index1>=0) && (index2>=0) && (index3>=0)) {
		text1=form.column1.options[index1].text;
		text2=form.column2.options[index2].text;
		text3=form.column3.options[index3].text;
		form.path.value=text1+" ==> "+text2+" ==> "+text3;
	} else
		form.path.value="";
}
</script>

<form name="theForm">
<table align=center border=1>
	<tr>
		<th>ºq¦±»y¨¥<th>ºq¬P¦W¦r<th>ºq¦±²M³æ
	<tr>
		<td align=center>
			<select name="column1" size=10 onChange="onChangeColumn1();">
				<script>
				for (i=0; i<dataTree.length; i++)
					document.writeln("<option value=\""+dataTree[i].name+"\">"+dataTree[i].name);
				</script>
			</select>
		<td align=center>
			<select name="column2" size=10 onChange="onChangeColumn2();">
			</select>
		<td align=center>
			<select name="column3" size=10 onChange="onChangeColumn3();">
			</select>
	<tr><td colspan=3 align=center>¸ô®|¡G<input type=text name=path size=60></td></tr>
</table>
</form>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î¤F¤@­Ó¦Û­qª«¥óªº«Øºc¨ç¼Æ node()¡A¥H«K¥Î¨Ó«Ø¥ß¦¹¤T¼h¾ðª¬¸ê®Æµ²ºc¡C¦¹¸ê®Æµ²ºc¥i¥HÅã¥Ü¦p¤U¡G

°ê»y --- ±i¾Ç¤Í --- §Úµ¥¨ìªá¨à¤]Á¤F --- ¤@¤d­Ó¶Ë¤ßªº²z¥Ñ --- ©@°Ø --- ±i´f©f --- Å¥®ü --- °Å·R --- ­ì¨Ó§A¤°»ò³£¤£­n --- ¤T¤Ñ¤T©] --- ¼B¼wµØ --- ÁÂÁ§Aªº·R --- ¨Ó¥Í½t --- §Ñ±¡¤ô --- ¥î¨Õ --- ®ö¤H±¡ºq --- ¾ðªK©t³¾ --- ©PµØ°· --- ªá¤ß --- ¤ßªº¤è¦V ­^»y --- Michael --- Beat It --- Billie Jean --- Heal The World --- Celine --- My Heart Will Go On --- Hope
JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ