<select name="select">
<option value="1111" <% If ors("field")="1111" Then Response.Write("selected") %>>111</option>
<option value="2222" <% If ors("field")="2222" Then Response.Write("selected") %>>222</option>
<option value="333" <% If ors("field")="333" Then Response.Write("selected") %>>333</option>
<option value="444" <% If ors("field")="444" Then Response.Write("selected") %>>444</option>
</select>碰上下拉菜单项非常多时,书写起来非常浪费时间,下边写了个JS函数用来实现转换,只需将原来的<select>复制粘贴到页面begin和end之间,点击转换按钮,页面即显示转换后的<select>,再复制粘贴回去即可:
<script language="javascript">
//==================================================
//函数名:toAsp
//作 用:对下拉菜单代码进行转换
//参 数:field ------数据库字段
//参 数:rs ------RecordSet对象
//返回值:在页面显示转换后的代码,复制即可使用
//说 明:必须将下拉菜单放在begin和end之间
//==================================================
function toAsp(field,rs){
var str = "";
var obj = document.forms[0].elements[0];
str = "&\lt;select name=\"" + obj.name + "\"></br>"
for(var i=0;i<=obj.length-1;i++){
str += "&\lt;option value=\"" + obj.options[i].value + "\" &\lt;% If " + rs + "(\"" + field + "\")=\"" + obj.options[i].value + "\" Then Response.Write(\"selected\") %>>" + obj.options[i].text + "&\lt;/option></br>";
}
str += "&\lt;/select>";
document.write(str);
}
</script>
<form id="form1" name="form1" method="post" action="">
<!-- Select begin -->
<select name="select">
<option value="1111">111</option>
<option value="2222">222</option>
<option value="3333">333</option>
<option value="4444">444</option>
</select>
<!-- Select end -->
<input name="btn" type="button" id="btn" value="转换" onclick="toAsp('field2','Rs');" />
</form>想看看动网是怎么解决这个问题的,打开mymodify.asp找了他的转换函数:
<%
Function Chk_select(str1,str2)
Dim k
str2=Split(str2,",")
If IsEmpty(str1) or str1="" Then chk_select="<option value='' selected>...</option>"
For k=0 to ubound(str2)
chk_select=chk_select+"<option value="+str2(k)
If str2(k)=str1 Then chk_select=chk_select+" selected "
chk_select=chk_select+" >"+str2(k)+"</option>"
Next
End Function
%>其中第一个参数为要选定的值,第二个参数为全部<select>项的值组成的字符串,各项值以逗号隔开,如:Chk_select("2222","1111,2222,3333,4444")。
评论(0)
暂无评论。