Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> two-dimensional function </title>
<script type= "Text/javascript" language= "JavaScript" >
/*
Formed an array of rowlength,collength
*/
function Darray (rowlength, collength) {
var darray = new Array (rowlength);
Add a data about collength to each column
for (var i = 0; i < rowlength; i++) {
Darray[i] = new Array (collength);
}
return darray;
}
function GetArray () {
var darray = new Darray (10, 10);
DARRAY[0][10] = 100;
DARRAY[0][1] = "I like JS";
Alert (darray[0][10] + "," + darray[0][1]);
}
</script>
<body>
<input type= "button" value= "Get Two-dimensional" onclick= "GetArray ()"/>
</body>
Binding a select Drop-down menu by declaring a two-dimensional array
Copy Code code as follows:
<HTML>
<HEAD>
<TITLE> Dynamic Change Drop-down Menu content Example </TITLE>
</HEAD>
<script language=javascript>
Defines a two-dimensional array aarray for storing city names.
var acity=new Array ();
Acity[0]=new Array ();
Acity[1]=new Array ();
Acity[2]=new Array ();
Acity[3]=new Array ();
Assignment, the city of each province is placed on one row of the array.
acity[0][0]= "--Please Choose-";
acity[1][0]= "--Please Choose-";
Acity[1][1]= "Guangzhou";
Acity[1][2]= "Shenzhen";
Acity[1][3]= "Zhuhai";
Acity[1][4]= "Shantou";
Acity[1][5]= "Foshan";
acity[2][0]= "--Please Choose-";
Acity[2][1]= "Changsha";
acity[2][2]= "Zhuzhou City";
Acity[2][3]= "Xiangtan";
acity[3][0]= "--Please Choose-";
acity[3][1]= "Hangzhou City";
Acity[3][2]= "Suzhou";
Acity[3][3]= "Wenzhou";
function changecity ()
{
var I,iprovinceindex;
Iprovinceindex=document.frm.optprovince.selectedindex;
icitycount=0;
while (Acity[iprovinceindex][icitycount]!=null)
icitycount++;
Calculate the number of cities in the selected province
document.frm.optcity.length=icitycount;//change the number of options for a drop-down menu
for (i=0;i<=icitycount-1;i++)//Change the contents of the Drop-down menu
Document.frm.optcity[i]=new Option (Acity[iprovinceindex][i]);
Document.frm.optCity.focus ();
}
</SCRIPT>
<body onfocus=changecity () >
<H3> Choose your province and city </H3>
<form name= "frm" >
<p> Province:
<select name= "Optprovince" size= "1" onchange=changecity () >
<option>--Please select--</option>
<option> </OPTION> of Guangdong Province
<option> </OPTION> of Hunan Province
<option> </OPTION> of Zhejiang Province
</select>
</p>
<p> City:
<select name= "Optcity" size= "1" >
<option>--Please select--</option>
</select>
</p>
</FORM>
</BODY>
</HTML>