当前位置:网站首页 / javascript / 正文
js实现select标签的默认选择
2019-06-27 / javascript / 951 次围观 / 0 次吐槽 /Markup
<script type="text/javascript">
function showDefaultCity(cityCode){
//获取select标签
var cityTag = document.getElementById("city");
//获取select下所有option
var options = cityTag.options;
//遍历option是否cityCode相等
for(var i=0;i<options.length;i++){
var optionTag = options[i];
//如果相等就选择
if(optionTag.value == cityCode){
//optionTag.selected = true;
optionTag.selected = 'selected';
return;
}
}
}
</script>
<body onload="showDefaultCity('sz')">
城市:<select id="city">
<option value="">--请选择--</option>
<option value="gz">广州</option>
<option value="sh">上海</option>
<option value="bj">北京</option>
<option value="sz">深圳</option>
<option value="cq">重庆</option>
<option value="tj">天津</option>
</select>
</body>
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.