Let's take a look at the effect:
Html source code:
Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> dynamic menu change </title>
<Script type = "text/javascript" src = "jquery. js"> </script>
<Script type = "text/javascript" src = "SelectMenu. js"> </script>
</Head>
<Body>
<Form action = "#">
<Br/>
<Br/>
<Br/>
<Div class = "Address">
<Span class = "Province"> Province:
<Select>
<Option value = "" selected = "selected"> Please Choose Province </option>
<Option value = "HeBei"> HeBei </option>
<Option value = "ShanDong"> ShanDong </option>
</Select>
</Span>
<Span class = "City" style = "display: none"> City:
<Select>
</Select>
</Span>
<Span class = "Area" style = "display: none"> Area:
<Select>
</Select>
</Span>
<Br/>
<Br/>
<Span class = "AddressSelect" style = "display: none">
</Span>
</Div>
</Form>
</Body>
</Html>
Javascript source codeCopy codeThe Code is as follows: $ (document). ready (function (){
// Find the three drop-down lists
Var ProvinceSelect = $ (". Province"). children ("select ");
Var CitySelect = $ (". City"). children ("select ");
Var AreaSelect = $ (". Area"). children ("select ");
Var AddressSelect = $ (". AddressSelect ");
// Register the event for the second drop-down box
ProvinceSelect. change (function (){
// 1. Obtain the value of the current drop-down box
Var ProvinceValue = $ (this). val ();
// 1.1 as long as the content of the first drop-down box changes, the third drop-down box will be hidden
AreaSelect. parent (). hide ();
AddressSelect. hide ();
AddressSelect.html ("");
// 2. If the value is not blank, the city drop-down list is displayed.
If (ProvinceValue! = ""){
CitySelect.html ("");
$ ("<Option value =''> Please Choose City </option> "). appendTo (CitySelect );
Switch (ProvinceValue)
{
// In the actual project, the city array must be obtained on the server. For convenience, I directly customized an array.
// If you want perfection, you can add a cache to prevent repeated access.
Case "HeBei ":
Var CityOfHeBei = ["ShiJiaZhuang", "CangZhou", "LangFang"];
For (var I = 0; I <CityOfHeBei. length; I ++ ){
$ ("<Option value = '" + CityOfHeBei [I] + "'>" + CityOfHeBei [I] + "</option>"). appendTo (CitySelect );
}
Break;
Case "ShanDong ":
Var CityOfShanDon = ["JiNan", "DeZhou", "QingDao"];
For (var I = 0; I <CityOfShanDon. length; I ++ ){
$ ("<Option value = '" + CityOfShanDon [I] + "'>" + CityOfShanDon [I] + "</option>"). appendTo (CitySelect );
}
Break;
}
CitySelect. parent (). show ();
} Else {
CitySelect. parent (). hide ();
}
});
// Register the event for the second drop-down box
CitySelect. change (function (){
Var CityValue = $ (this). val ();
AddressSelect. hide ();
AreaSelect. parent (). hide ();
AddressSelect.html ("");
If (CityValue! = ""){
AreaSelect.html ("");
$ ("<Option value =''> Please Choose Area </option> "). appendTo (AreaSelect );
Switch (CityValue)
{
// In the actual project, the array in this area must be obtained on the server. For convenience, I have directly customized an array.
// If you want perfection, you can add a cache to prevent repeated access.
Case "ShiJiaZhuang ":
Var AreaOfCity = ["GaoXinQu", "KaiFaQu", "XinHuaQu"];
For (var I = 0; I <AreaOfCity. length; I ++ ){
$ ("<Option value = '" + AreaOfCity [I] + "'>" + AreaOfCity [I] + "</option>"). appendTo (AreaSelect );
}
Break;
Case "CangZhou ":
Var AreaOfCity = ["XinHuaQu", "YunHeQu"];
For (var I = 0; I <AreaOfCity. length; I ++ ){
$ ("<Option value = '" + AreaOfCity [I] + "'>" + AreaOfCity [I] + "</option>"). appendTo (AreaSelect );
}
Break;
Case "LangFang ":
Var AreaOfCity = ["AnCiQu", "GuangYangQu"];
For (var I = 0; I <AreaOfCity. length; I ++ ){
$ ("<Option value = '" + AreaOfCity [I] + "'>" + AreaOfCity [I] + "</option>"). appendTo (AreaSelect );
}
Break;
Case "QingDao ":
Var AreaOfCity = ["GaoXinQu", "KaiFaQu", "XinHuaQu"];
For (var I = 0; I <AreaOfCity. length; I ++ ){
$ ("<Option value = '" + AreaOfCity [I] + "'>" + AreaOfCity [I] + "</option>"). appendTo (AreaSelect );
}
Break;
Case "DeZhou ":
Var AreaOfCity = ["XinHuaQu", "YunHeQu"];
For (var I = 0; I <AreaOfCity. length; I ++ ){
$ ("<Option value = '" + AreaOfCity [I] + "'>" + AreaOfCity [I] + "</option>"). appendTo (AreaSelect );
}
Break;
Case "JiNan ":
Var AreaOfCity = ["AnCiQu", "GuangYangQu"];
For (var I = 0; I <AreaOfCity. length; I ++ ){
$ ("<Option value = '" + AreaOfCity [I] + "'>" + AreaOfCity [I] + "</option>"). appendTo (AreaSelect );
}
Break;
}
AreaSelect. parent (). show ();
} Else {
AreaSelect. parent (). hide ();
}
});
AreaSelect. change (function (){
Var AreaValue = $ (this). val ();
AddressSelect.html ("");
If (AreaValue! = ""){
$ ("<Span> The Address Is -- Province:" + ProvinceSelect. val () + "City:" + CitySelect. val () + "Area:" + AreaSelect. val () + "</span> "). appendTo (AddressSelect );
AddressSelect. show ();
// Alert ("The Address Is Province:" + ProvinceSelect. val () + "City:" + CitySelect. val () + "Area:" + AreaSelect. val ());
}
})
});
Jquery is also referenced here. It seems that it doesn't matter if you don't need it. Recently, it was added to familiarize yourself with Jquery's usage.