<select name="department" id="department">
<%
List departments = null;
if (request.getAttribute("departments") != null) {
departments = (List) request.getAttribute("departments");
}
if (departments != null && !departments.isEmpty()) {
for (Iterator iter = departments.iterator(); iter.hasNext()); ) {
Department department = (Department) iter.next();
%>
<option value="<%=department.getId()%>" <%if (department.getId() == 2) out.print("selected");%>><%=department.getName()%></option>
<%
}
}
%>
</select>
<%
List departments = null;
if (request.getAttribute("departments") != null) {
departments = (List) request.getAttribute("departments");
}
if (departments != null && !departments.isEmpty()) {
for (Iterator iter = departments.iterator(); iter.hasNext()); ) {
Department department = (Department) iter.next();
%>
<option value="<%=department.getId()%>" <%if (department.getId() == 2) out.print("selected");%>><%=department.getName()%></option>
<%
}
}
%>
</select>
<HTML>
<HEAD>
<TITLE>Example</TITLE>
</HEAD>
<SCRIPT>
function selectitem (i){
form1.department.selectedIndex=i;
}
</SCRIPT>
<BODY onload="selectitem(2)">
<FORM NAME="form1">
<select name="department" id="department">
<option value="1" >发展和改革局</option>
<option value="2" >贸易工业局</option>
<option value="3">教育局</option>
<option value="4">科技和信息局</option>
<option value="5" selected>民族宗教事务局</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>
</FORM>
</BODY>
</HTML>注意selectedIndex从0开始计数
<BODY onload="selectitem(<%=dep%>)">