JS对select动态添加options操作[IE&FireFox兼容]

<select id="ddlResourceType" onchange="getvalue(this)">
</select> 动态删除select中的所有options:
document.getElementById("ddlResourceType").options.length=; 动态删除select中的某一项option:
document.getElementById("ddlResourceType").options.remove(indx); 动态添加select中的项option:
document.getElementById("ddlResourceType").options.add(new Option(text,value)); 上面在IE和FireFox都能测试成功,希望以后你可以用上。
其实用标准的DOM操作也可以,就是document.createElement,appendChild,removeChild之类的。 取值方面
function getvalue(obj)
{
var m=obj.options[obj.selectedIndex].value
alert(m);//获取value
var n=obj.options[obj.selectedIndex].text
alert(n);//获取文本
} ==============================================================================
检测是否有选中
if (objSelect.selectedIndex > - ) {
// 说明选中
} else {
// 说明没有选中
} 删除被选中的项
objSelect.options[objSelect.selectedIndex] = null ; 增加项
objSelect.options[objSelect.length] = new Option( " 你好 " , " hello " ); 修改所选择中的项
objSelect.options[objSelect.selectedIndex] = new Option( " 你好 " , " hello " ); 得到所选择项的文本
objSelect.options[objSelect.selectedIndex].text; 得到所选择项的值
objSelect.options[objSelect.selectedIndex].value; ====================================================================== <%@LANGUAGE="VBSCRIPT" CODEPAGE=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS对select动态添加options操作[IE和FireFox兼容]</title> <script language="javascript" type="text/javascript">
function xlbchange(s){ switch (s){
case "" :
document.getElementById("lb").options.length=;
var soojs_value=[,,,];
var soojs_text=["精神提炼","作风设计","目标设置","理念提升"];
for ( var i=;i<soojs_value.length;i++){
var oOption = document.createElement("OPTION");
oOption.value=soojs_value[i];
oOption.text=soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
case "" :
document.getElementById("lb").options.length=;
var soojs_value=[,,,,,,,];
var soojs_text=["校徽","校训","校歌","校名字体","校史展室","宣传画册","宣传光盘","办公用品纪念品"];
for ( var i=;i<soojs_value.length;i++){
var oOption = document.createElement("OPTION");
oOption.value=soojs_value[i];
oOption.text=soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
case "":
document.getElementById("lb").options.length=;
var soojs_value=[,,,];
var soojs_text=["校园景观设计","校园雕塑设计","校园浮雕设计","走廊文化设计"];
for ( var i=;i<soojs_value.length;i++){
var oOption = document.createElement("OPTION");
oOption.value=soojs_value[i];
oOption.text=soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
case "":
document.getElementById("lb").options.length=;
var soojs_value=[,,];
var soojs_text=["学校制度","文化活动","行为规范"];
for ( var i=;i<soojs_value.length;i++){
var oOption = document.createElement("OPTION");
oOption.value=soojs_value[i];
oOption.text=soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
default :
document.getElementById("lb").options.length=;
var oOption = document.createElement("OPTION");
oOption.value=;
oOption.text="请选择作品类别";
zpmange.lb.options.add(oOption);
}
}
</script>
</head>
<body>
<form action="zpmange.asp" name="zpmange" method="post">
<p><select id="ddlResourceType" onchange="getvalue(this)"><br />
</select></p>
<p> 动态删除select中的所有options: <br />
document.getElementById("ddlResourceType").options.length=; <br />
<br />
动态删除select中的某一项option: <br />
document.getElementById("ddlResourceType").options.remove(indx); </p>
<p> 动态添加select中的项option: <br />
document.getElementById("ddlResourceType").options.add(new Option(text,value)); </p>
<p> 上面在IE和FireFox都能测试成功,希望以后你可以用上。 <br />
其实用标准的DOM操作也可以,就是document.createElement,appendChild,removeChild之类的。 </p>
<p>取值方面<br />
function getvalue(obj)<br />
{<br />
var m=obj.options[obj.selectedIndex].value<br />
alert(m);//获取value<br />
var n=obj.options[obj.selectedIndex].text<br />
alert(n);//获取文本<br />
}<br />
</p>
<p>例子:</p>
<table width="80%" border="" align="center" cellpadding="" cellspacing="" style="border-style:double">
<tr>
<td width="50%" height="" ><div align="center"> <select name="xt" id="xt" onchange="xlbchange(this.value)">
<option selected="selected">请选择作品系统</option>
<option value="">理念视别系统</option>
<option value="">视觉识别系统</option>
<option value="">环境视别系统</option>
<option value="">行为视别系统</option>
</select> </div></td>
<td width="61%" ><div align="center">
<select name="lb" id="lb">
<option selected="selected">请选择作品类别</option> </select>
</div></td>
</tr>
<tr>
<td height="" colspan="" > </td>
</tr>
</table>
</form>
</body>
</html>
上一篇:Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embe


下一篇:【JavaService】部署Java jar为Windows后台服务