html下拉菜单

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<select id="province">
<option value ="">北京</option>
</select>
</body>
<script type="text/javascript">
//设置城市下拉菜单
setProvinceData();
function setProvinceData(){
var arr = ["请选择","北京","黑龙江","浙江","江苏","江西","广东","河南","内蒙"];
//拼接option
var allhtml = ""
for(var i=0;i<arr.length;i++){
var obj = arr[i]
item = "<option value = ‘"+i+"‘>"+obj+"</option>";//<option value ="1">北京</option>
allhtml = allhtml + item;
}
document.getElementById("province").innerHTML = allhtml;
}

var provinceEle = document.getElementById("province");
//下拉菜单 改变的时候会触发
provinceEle.addEventListener("change",function(){
var index = provinceEle.selectedIndex;//选择的序号
var value = provinceEle.options[index].value;
var text = provinceEle.options[index].text;
alert(index + "->" + text)
});
</script>
</html>

html下拉菜单

上一篇:css盒模型以及如何计算盒子的宽度


下一篇:POJ 3659 Cell Phone Network