傳統的HTML頁面中連動下拉框采用了兩種方法:
1)直接將下拉框中的內容hardcode于html的javascript中,調用javascript函數循環寫入下拉框中。這種方法不適用于下拉框內容經常改變的情況。因為數據源和javascript程序寫死在同一頁面。
<html>
<head>
<title>List</title>
<meta http-equiv="Content-Type" content="text/html; c
harset=gb2312">
<script LANGUAGE="javascript">
<!--
var onecount;
onecount=0;
subcat = new Array();
subcat[0] = new Array("徐匯區","01","001");
subcat[1] = new Array("嘉定區","01","002");
subcat[2] = new Array("黃浦區","01","003");
subcat[3] = new Array("南昌市","02","004");
subcat[4] = new Array("九江市","02","005");
subcat[5] = new Array("上饒市","02","006");
onecount=6;
function changelocation(locationid)
{
document.myform.smalllocation.length = 0;
var locationid=locationid;
var i;
document.myform.smalllocation.options[0] = new Option('====所有地區====','');
for (i=0;i <onecount; i++)
{
if (subcat[i][1] == locationid)
{
document.myform.smalllocation.options[document.myform.smalllocation.length]
= new Option(subcat[i][0], subcat[i][2]);
}
}
}
//-->
</script>
</head>
<body>
<form name="myform" method="post">
<select name="biglocation"
onChange="changelocation(document.myform.biglocation.options[document.myform.biglocation.selectedIndex].value)">
<option value="01" selected>上海</option>
<option value="02">江西</option>
</select>
<select name="smalllocation">
<option selected value="">==所有地區==</option>
</select>
</form>
<script LANGUAGE="javascript">
<!--
changelocation(document.myform.biglocation.options[document.myform.biglocation.selectedIndex].value);
//-->
</script>
</body>
</html>
2)javascript 直接讀取數據庫,取數據庫中的記錄寫入javascript中,然后和第一種方法一樣,調用javascript函數循環寫入下拉框中。此方法將數據源與javascript分開,但,公開數據庫的連接,從安全角度說,沒有多少實用價值。
我的方法是將下拉框中的數據放在XML文件中,用javascript讀XML文件,取得下拉框中的內容。
HTML 文件如下:
<!-- myfile.html -->
<html>
<head>
<script language="JavaScript" for="window" event="onload">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var i=0;
var j=0;
var subclass_name="";
loadXML();
function loadXML(){
xmlDoc.async="false";
xmlDoc.load("account.xml");
xmlObj=xmlDoc.documentElement;
nodes = xmlDoc.documentElement.childNodes;
document.frm.mainclass.options.length = 0;
document.frm.subclass.options.length = 0;
新聞熱點
疑難解答