日期:2014-05-17  浏览次数:21161 次

ASP用javascript编写的下拉条,刷新后怎样保持选中?
代码如下,该下拉条是为用户选中月份,点击确定后查询出数据,此时该下拉条又恢复为初始值1,不能保持之前用户选中的数字,请问各位大神如何解决啊!!!谢谢!!!
<select name="month1" style="width:40px">
<script type="text/javascript">
var i=1
for (i=1;i<=12;i++)
{
document.write("<option>"+ i +"</option>")
}
</script> </select>&nbsp;/&nbsp;

------解决方案--------------------
在后台写注册一个前台脚本 string the_js_string = "var selectID = " + selectID;
其中select是你提交到后台的值。
前台。

var o = document.getElementById("month1").options;

for(var i=0,n=o.length ;i<n;i++){
if(selectID ==o[i].value){o[i].selected=true;return;}
}

------解决方案--------------------
非常不好意思,我以为是asp.net呢。
asp的话非常简单啊(我没测试可能有小问题,大概意思如下)

<%
dim selectID = request["month1"]
%>

<select name="month1" style="width:40px">
<script type="text/javascript">
var js_selectID = <%=selectID%>
var i=1
for (i=1;i<=12;i++)
{
if(i==js_selectID )
{
document.write("<option selected>"+ i +"</option>")
}else
{
document.write("<option>"+ i +"</option>")
}
}
</script> </select>&nbsp;/&nbsp;