1
 <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>
     </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]);

4 Answers 4

3

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]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

The variable city is a list of lists, showing which province has which cities. The first list is city[0]. But selectedIndex for the first one is 1.
could i change this sltCity[i+1] to sltCity[i]?
no.because there is a sltCity[0],which is "select city". am i right?
1

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.

Comments

1

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.

Comments

1

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>

1 Comment

could i change this sltCity[i+1] to sltCity[i]? no.because there is a sltCity[0],which is "select city". am i right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.