傳統(tǒng)的HTML頁(yè)面中連動(dòng)下拉框采用了兩種方法:
1)直接將下拉框中的內(nèi)容hardcode于html的javascript中,調(diào)用javascript函數(shù)循環(huán)寫入下拉框中。這種方法不適用于下拉框內(nèi)容經(jīng)常改變的情況。因?yàn)閿?shù)據(jù)源和javascript程序?qū)懰涝谕豁?yè)面。
<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("徐匯區(qū)","01","001");
subcat[1] = new Array("嘉定區(qū)","01","002");
subcat[2] = new Array("黃浦區(qū)","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('====所有地區(qū)====','');
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="">==所有地區(qū)==</option>
</select>
</form>
<script LANGUAGE="javascript">
<!--
changelocation(document.myform.biglocation.options[document.myform.biglocation.selectedIndex].value);
//-->
</script>
</body>
</html>
2)javascript 直接讀取數(shù)據(jù)庫(kù),取數(shù)據(jù)庫(kù)中的記錄寫入javascript中,然后和第一種方法一樣,調(diào)用javascript函數(shù)循環(huán)寫入下拉框中。此方法將數(shù)據(jù)源與javascript分開(kāi),但,公開(kāi)數(shù)據(jù)庫(kù)的連接,從安全角度說(shuō),沒(méi)有多少實(shí)用價(jià)值。
我的方法是將下拉框中的數(shù)據(jù)放在XML文件中,用javascript讀XML文件,取得下拉框中的內(nèi)容。
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;
新聞熱點(diǎn)
疑難解答
圖片精選