修改资料时设定下拉菜单某个值为选中状态

在修改资料时,为方便用户操作,必须将先前选取的下拉菜单的某个值设定为选中状态,通常我们用ASP书写如下代码:

<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>,再复制粘贴回去即可:

HTML代码


[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

想看看动网是怎么解决这个问题的,打开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 | 引用: 0 | 查看次数: 4892
发表评论
登录后再发表评论!