[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[ASP]网页后退一步时,表单中select下拉框的值的恢复的解决方法

上一篇:[整理]日语的文字编码
下一篇:[备忘]使用Excel另存为xml文件,替换内容动态生成Excel文件的一点注意事项

添加日期:2007/8/22 9:59:13 快速返回   返回列表 阅读5983次


<%
toDoAction = Trim(Request.Form("toDoAction"))
If toDoAction="edit" Then
    ID=Trim(Request.Form("ID")
    '取得数据
    .............
    aa=rs("aa")
    bb=rs("bb")
    ...
End If
%>
<select name="aa" id="aa">
<option value=""></option>
<option value="1">娃哈哈</option>
<option value="2">娃嘎嘎</option>
</select>

<script>
function setSelectByValue(objectID,inValue){
    var obj=document.getElementById(objectID); 
    for(var i=0;i<obj.options.length;i++){
        if(obj.options[i].value==inValue){
            obj.options[i].selected=true;
            break;
        }
    }
}
setSelectByValue("aa","<%=aa%>");
</script>



比如DB的值是1,那么此页面初次进入时,“娃哈哈”处于选择状态。
现在选择“娃嘎嘎”,提交。当处理页面报check,使用history.go(-1)后退时,会发现,
“娃嘎嘎”没有处于选择状态,恢复了初始的“娃哈哈”。

想来想去,又折腾半天,

觉得原因是:

页面后退时,浏览器将源代码重新执行了一遍,然后恢复用户所输入的值。

但是恢复的时机是不同的,

select框的值在它被载入的时候,已经是用户所选择的了,

而text框,checkbox,radio等的值,是在页面载入之后才恢复的。


这就是上面代码中select框的选择状态被恢复为DB初始值的原因,

因为我们的脚本在输入值被恢复之后执行的。
------------------------------------------------------
解决办法,自然想到:

提交表单时,用隐藏框保存select框所选择的值,页面载入之后再恢复。


<form action="" name="inputForm" id="inputForm" method="post" onSubmit="saveDropDownSelected()">
...
<input type="hidden" name="aa_Hidden" id="aa_Hidden" value="<%=aa%>">
</form>

<script>
function saveDropDownSelected(){
    $("aa_Hidden").value=$("aa").options[$("aa").selectedIndex].value; 
}

function resetDropDownSelected(){
    setSelectByValue("aa",$("aa_Hidden").value); 
}
resetDropDownSelected();
</script>



其中的$就是document.getElementById()的意思。

开始是在页面最底部使用resetDropDownSelected()的,但是不好使…………

Right,时机不对,注意text框等的值被恢复的时机是页面整个被载入之后,也就是</html>之后,

所以这之前的脚本都不行。

最后使用


<script>
window.onload=resumePage;

function resumePage(){
    resetDropDownSelected();
    //其他要恢复的
}
function saveDropDownSelected(){
    $("aa_Hidden").value=$("aa").options[$("aa").selectedIndex].value; 
}

function resetDropDownSelected(){
    setSelectByValue("aa",$("aa_Hidden").value); 
}
</script>



搞定。
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved