<select>赋值问题 现在有个List集合,其中存有数据,如何在页面中把List集合里的数据赋给<select>,其中<select>选项中包含List的值 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你知道怎么遍历List中的元素吧?做个循环,挨个赋值给option的value不就行了. <% List listLocation = .....;%> <select name="location" <%for (Iterator iter = listLocation.iterator(); iter.hasNext();) { String locationName = (String) iter.next(); %> <option value="<%=locationName%>" ><%=locationName%></option> <%}%> </select> <% List listLocation = .....;%> <select name="location" > <%for (Iterator iter = listLocation.iterator(); iter.hasNext();) { String locationName = (String) iter.next(); %> <option value="<%=locationName%>" ><%=locationName%></option> <%}%> </select> 我的意思是,原来<select>中就有数据,并且这些数据也是通过循环赋上去的,现在在后台传过来一个List,List中存的数据在<select>中已经有了,现在的问题就是如何让List中存的数据在<select>中默认为选中状态!!List存的是字符串 <option <%if(list的值.equals("aaa")) out.println("selected");%>>aaa</option> 你在你在循环里面加个判断语句,if(相同)<option value="<%=locationName%>" "selected"><%=locationName%></option>else<option value="<%=locationName%>" ><%=locationName%></option>具体例子我给你个我写的类似的吧。<select name="prd_no" class="other"> <% sql="select prd_no from robin_prd"; rs=DB.executeQuery(sql); String o_prd_no=prd_no; while(rs.next()) { prd_no=rs.getString(1); out.print("<option value="+prd_no+" class=other "); if(prd_no.equals(o_prd_no)) out.print("selected"); out.print(" >"+prd_no+"</option>"); }%> </select>代码质量可能不高,请见谅! List list = tsakNamelist[i]; Iterator it1 = list.iterator(); out.print("<td width='150' align='center'>");%> <select name='task_id' style='width:120' onchange='selectChange(<%=j%>,this.value)'> <%out.print("<option value='null'>请选择...</option>"); while(it1.hasNext()){ DayReportDto dayReportDto = (DayReportDto)it1.next(); out.print("<option value="+dayReportDto.getTask_id()); for(int k=0;k<task_id.length;k++){ if(dayReportDto.getTask_id().equals(task_id[k])) out.print(" selected>"); } out.print(dayReportDto.getTask_name()); out.print("</option>"); } out.print("</select>"); out.print("</td>");其中while循环是把数据库中的值邦定到<select>过程,for是把从后台传来的String[]进行与邦定值比较,为什么还是不行,那位帮忙看看 out.print(" selected>");改为out.print(" selected >");试下。 com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭。 分页导航 求助:中文参数传递问题 tomcat的问题 我使用WEB,如何在程序中,获取CPU、硬盘、IP地址及其它信息? 急求一条SQL语句!!! 如何把网站的首页面做成静态的!!! 求关于模拟演练程序开发的思路 jb9中自定义tomcat的server.xml的问题 关于jsp和js的!在线! 谁会以下问题的算法!! 调查一下现在jstl用的多吗?
做个循环,挨个赋值给option的value不就行了.
List listLocation = .....;
%>
<select name="location"
<%for (Iterator iter = listLocation.iterator(); iter.hasNext();) {
String locationName = (String) iter.next(); %>
<option value="<%=locationName%>" ><%=locationName%></option>
<%}%>
</select>
List listLocation = .....;
%>
<select name="location" >
<%for (Iterator iter = listLocation.iterator(); iter.hasNext();) {
String locationName = (String) iter.next(); %>
<option value="<%=locationName%>" ><%=locationName%></option>
<%}%>
</select>
if(相同)
<option value="<%=locationName%>" "selected"><%=locationName%></option>
else
<option value="<%=locationName%>" ><%=locationName%></option>
具体例子我给你个我写的类似的吧。
<select name="prd_no" class="other">
<%
sql="select prd_no from robin_prd";
rs=DB.executeQuery(sql);
String o_prd_no=prd_no;
while(rs.next())
{
prd_no=rs.getString(1);
out.print("<option value="+prd_no+" class=other ");
if(prd_no.equals(o_prd_no))
out.print("selected");
out.print(" >"+prd_no+"</option>");
}
%>
</select>
代码质量可能不高,请见谅!
Iterator it1 = list.iterator();
out.print("<td width='150' align='center'>");%>
<select name='task_id' style='width:120' onchange='selectChange(<%=j%>,this.value)'>
<%out.print("<option value='null'>请选择...</option>");
while(it1.hasNext()){
DayReportDto dayReportDto = (DayReportDto)it1.next();
out.print("<option value="+dayReportDto.getTask_id());
for(int k=0;k<task_id.length;k++){
if(dayReportDto.getTask_id().equals(task_id[k]))
out.print(" selected>");
}
out.print(dayReportDto.getTask_name());
out.print("</option>");
}
out.print("</select>");
out.print("</td>");其中while循环是把数据库中的值邦定到<select>过程,for是把从后台传来的String[]进行与邦定值比较,为什么还是不行,那位帮忙看看
改为
out.print(" selected >");试下。