jsp代码:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- 声明变量用于接收参数 -->
<%!String year;
String month;%>
<!--接收客户端传过来的参数 -->
<%
month = request.getParameter("month");
year = request.getParameter("year");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>羊咩咩日历</title> <script type="text/javascript">
function changeMonth(){
//document.sm.month.options.selectedIndex=<%=month%>
var mm = "calendar.jsp?month="+document.sm.month.options.selectedIndex+"&year"+<%=year%>;
window.open(mm,"_self");
}
</script>
</head>
<%!String days[];%>
<%
days = new String[42];
for (int i = 0; i < 42; i++) {
days[i] = "";
}
%> <%
//默认为当天
Calendar thisMonth = Calendar.getInstance();
//如果有参数传入,则重新设置月份年份
if (month != null && (!month.equals("null")))
thisMonth.set(Calendar.MONTH, Integer.parseInt(month));
if (year != null && (!year.equals("null")))
thisMonth.set(Calendar.YEAR, Integer.parseInt(year));
//重新获取月份年份
year = String.valueOf(thisMonth.get(Calendar.YEAR));
month = String.valueOf(thisMonth.get(Calendar.MONTH));
//设置每周第一天为周日
thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
//每个月第一天标记为1
thisMonth.set(Calendar.DAY_OF_MONTH, 1);
//求这个月第一天是星期几
int firstIndex = thisMonth.get(Calendar.DAY_OF_WEEK) - 1;
//求这个月多少天
int maxIndex = thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 0; i < maxIndex; i++) {
days[firstIndex + i] = String.valueOf(i + 1);
}
%>
<body>
<form name="sm" method="post" action="calendar.jsp">
<%=year%>年<%=Integer.parseInt(month) + 1%>
<table border="0" width="168" heitht="81">
<div align=center>
<tr>
<th width="25" height="16" bgcloor="#ffff00">
<font color="red">日</font>
</th>
<th width="25" height="16" bgcloor="#ffff00">

</th>
<th width="25" height="16" bgcloor="#ffff00">

</th>
<th width="25" height="16" bgcloor="#ffff00">

</th>
<th width="25" height="16" bgcloor="#ffff00">

</th>
<th width="25" height="16" bgcloor="#ffff00">

</th>
<th width="25" height="16" bgcloor="#ffff00">
<font color="green">六</font>
</th>
</tr>
<%
for (int j = 0; j < 6; j++) {
%>
<tr>
<%
for (int i = j * 7; i < (j + 1) * 7; i++) {
%>
<td width="15%" height="16" bgcolor="#c0c0c0" valign="middle"
align="center"><%=days[i]%></td>
<%
}
%>
</tr>
<%
}
%>
</div>
</table> <table border="0" width="168" height="20">
<tr>
<td width="30%">
<select name="month" size="1" onchange="changeMonth()">
<option value="0">
一月
</option>
<option value="1">
二月
</option>
<option value="2">
三月
</option>
<option value="3">
四月
</option>
<option value="4">
五月
</option>
<option value="5">
六月
</option>
<option value="6">
七月
</option>
<option value="7">
八月
</option>
<option value="8">
九月
</option>
<option value="9">
十月
</option>
<option value="10">
十一月
</option>
<option value="11">
十二月
</option>
</select>
</td>
<td width="28%">
<input type=text name="year" value=<%=year%> size=4 maxlength=4>
</td>
<td>

</td>
<td width="28%">
<input type=submit value="提交">
</td>
</tr>
</table>
</form>
<script type="text/javascript">
document.sm.month.options.selectedIndex=<%=month%>;
</script> </body>
</html>
想问的是,为什么document.sm.month.options.selectedIndex=<%=month%>;这条语句不能放在第一个<script>里面,而必须放在最后面的<script>里面jsp javascriptweb

解决方案 »

  1.   

    如果你放在第一个里面,就需要定义成一个函数,然后在onload里面调用才行。
    否则的话,就会导致,你页面还没有加载完成,你的js就调用了,最终的结果就是报错,对象未定义。
      

  2.   

    页面是从上至下加载的,
    你其实完全可以吧script都放在最下面。
    如果想放在最上面的话,就像楼上说的,加一个window.onload()={
    }
    才可以。
    意思就是等页面都加载完了再调用
      

  3.   

    需要在加载页面的时候就要执行的,必须放在onload中
      

  4.   

    上面说的都对,可以放到第一个JS里面,不过要在function外面,在里面不会被预加载
      

  5.   

    是因为你还没有加载到多选的那个对象呢。。放上面肯定找不到元素。不信你放到你那元素下面肯定好使。不放onload貌似也会被执行吧、、我一直都不放。。