我想要创建一个下拉菜单,该菜单会根据所选值生成另一个下拉菜单 例如,如果选择美国从第一次下降然后第二次下拉包含美国的状态。 我做了很多工作。我更喜欢使用javascript并使用数组
var country_arr = new Array("USA", "Singapore", "Pakistan")
var s_a = new Array();
s_a[0]="";
s_a[1]="CA|NJ|NY";
s_a[2]="paas|naas|taas";
s_a[3]="Islamabad|karachi|lahore";
但我也希望选择国家和州的“名称”,以便发送到mysql数据库。 我想我必须使用双维数组。但是这次无法编码并且非常需要你的帮助。
答案 0 :(得分:4)
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var citiesByState = {
USA: ["NY","NJ"],
Singapore: ["taas","naas"]
}
function makeSubmenu(value) {
if(value.length==0) document.getElementById("citySelect").innerHTML = "<option></option>";
else {
var citiesOptions = "";
for(cityId in citiesByState[value]) {
citiesOptions+="<option>"+citiesByState[value][cityId]+"</option>";
}
document.getElementById("citySelect").innerHTML = citiesOptions;
}
}
function displaySelected() {
var country = document.getElementById("countrySelect").value;
var city = document.getElementById("citySelect").value;
alert(country+"\n"+city);
}
function resetSelection() {
document.getElementById("countrySelect").selectedIndex = 0;
document.getElementById("citySelect").selectedIndex = 0;
}
</script>
</head>
<body onload="resetSelection()">
<select id="countrySelect" size="1" onchange="makeSubmenu(this.value)">
<option></option>
<option>USA</option>
<option>Singapore</option>
</select>
<select id="citySelect" size="1">
<option></option>
</select>
<button onclick="displaySelected()">show selected</button>
</body>
</html>