开发者

some code's meaning of javascript?

 <script language="JavaScript" type="text/javascript">     
     var city=[
     ["city1","city2","city3","city4"],
    ["city5","city6","city7"],
     ["city8","city9","city10"],
     ];

     function getCity(){            
         var sltProvince=document.form1.province;

        var sltCity=document.form1.city;             

         var provinceCity=city[sltProvince.selectedIndex - 1];     

         sltCity.length=1;    

         for(var i=0;i<provinceCity.length;i++){
             sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
         }
     }
</script>
 <FORM METHOD=POST ACTION="" name="form1">
         <SELECT NAME="province" onChange="getCity()">
             <OPTION VALUE="0">select province </OPTION>
             <OPTION VALUE="province1">province 1 </OPTION>
             <OPTION VALUE="province2">province2</OPTION>
             <OPTION VALUE="province3">province3 </OPTION>
         </SELECT>
         <SELECT NAME="city">
            <OPTION VALUE="0">select the city</OPTION>
        </SELECT>
开发者_JAVA百科     </FORM>

the above code is according to the province select its city. there are some lines i don't understand well. expect some one can explain it. thank you.

1, what's these lines do and meaning?

var provinceCity=city[sltProvince.selectedIndex - 1];

and

  sltCity.length=1;  
  sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);


It updates the list of cities based on the province that was selected.

// Get list of cities in the province, based on the index of the selected province.
// -1 is because the first entry in the list of city lists is 0, but in the province 
// options the first one has index 1.
var provinceCity=city[sltProvince.selectedIndex - 1];

// Remove all options except the first one that says "select city"
sltCity.length=1; 

// Add all the cities for this province to <SELECT NAME="province">
for(var i=0;i<provinceCity.length;i++){
    // add an option to the  with value and contents set to provinceCity[1]:
    // HTML: <OPTION VALUE="provinceCity[1]">provinceCity[1]</OPTION>
    sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
}


1, what's these lines do and meaning?

var provinceCity=city[sltProvince.selectedIndex - 1];

-- This means that provinceCity is created as a variable holds a value of a ComboBox which is been selected at that time.

sltCity.length=1;
....
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);

Once the province is selected the Script is loading the Cities in that province. Option means that you are creating new Drop Down option for the interface.


Pretty much this is adding city s to the city drop-down based upon the selection made in the first drop-down for provinces. var provinceCity=city[sltProvince.selectedIndex - 1]; is essentially grabbing the list of cities in the city array that is located at the array index which is one less than the index of the selected province.


First thing to understand is that city is an array of arrays. Therefore,

var provinceCity=city[sltProvince.selectedIndex - 1];

Sets provinceCity to the value of city's index at sltProvince's selected index (the index of the item selected) minus 1. Therefore, if the selected index is 3, provinceCity would equal city[2] or

["city5","city6","city7"]

This will ultimately effect the length of the for loop.

sltCity.length=1;

Sets the length of sltCity to 1.

sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);

Sets the value of the city array at the value of i (from the for loop) plus 1 to a new instance of Option, which is javascript's way for creating an option tag:

<OPTION> </OPTION>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜