<!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>
相关文章
- 10-24html或者jsp页面刷新问题
- 10-24HTML在网页上不能显示图片问题
- 10-24HTML5 Canvas合成
- 10-24HTML5---15.网络接口
- 10-24MxCAD云图 2020.09.28更新,前台使用 html5 方式显示 CAD
- 10-24HTML的Doctype
- 10-24html绝对路径与绝对路径表示法
- 10-24前端知识点总结(HTML篇)
- 10-24什么是html?
- 10-24学习笔记(01):8小时学会HTML网页开发-附录⑤ oveflow溢出处理