<html>
<body>
<script type="text/javascript">
function createOptions(){
var mySelect = document.getElementById("mySelect");
var group=document.createElement('OPTGROUP');
group.label = "JS创建的optgroup";
mySelect.appendChild(group);
mySelect.options.add(new Option("全部","all"));
mySelect.options.add(new Option("上海市","sh"));
mySelect.options.add(new Option("北京市","bj"));
var defaultSelect = mySelect.options[2];//参数只能是index,从0开始
defaultSelect.selected = true;
}
</script>
<form>
<select style="width:150px">
<optgroup label="--请选择">
<option value="0">全部</option>
<option value="1">上海市</option>
<option value="2">北京市</option>
</select>
</BR></BR></BR>
<select id="mySelect" name="mySelect" style="width:150px"></select>
<input type="button" onclick="createOptions()" value="js创建optgroup" />
</form>
</body>
</html>
html select的动态创建和optgroup属性
最新推荐文章于 2024-09-10 10:55:09 发布