现在有个List集合,其中存有数据,如何在页面中把List集合里的数据赋给<select>,其中<select>选项中包含List的值

解决方案 »

  1.   

    你知道怎么遍历List中的元素吧?
    做个循环,挨个赋值给option的value不就行了.
      

  2.   

    <%
      List listLocation = .....;
    %>
           <select name="location" 
                 <%for (Iterator iter = listLocation.iterator(); iter.hasNext();) {
                    String  locationName = (String) iter.next(); %>
                   <option value="<%=locationName%>" ><%=locationName%></option>
                 <%}%>
            </select>
      

  3.   

    <%
      List listLocation = .....;
    %>
           <select name="location" >
                 <%for (Iterator iter = listLocation.iterator(); iter.hasNext();) {
                    String  locationName = (String) iter.next(); %>
                   <option value="<%=locationName%>" ><%=locationName%></option>
                 <%}%>
            </select>
      

  4.   

    我的意思是,原来<select>中就有数据,并且这些数据也是通过循环赋上去的,现在在后台传过来一个List,List中存的数据在<select>中已经有了,现在的问题就是如何让List中存的数据在<select>中默认为选中状态!!List存的是字符串
      

  5.   

    <option <%if(list的值.equals("aaa")) out.println("selected");%>>aaa</option>
      

  6.   

    你在你在循环里面加个判断语句,
    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>
    代码质量可能不高,请见谅!
      

  7.   

    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[]进行与邦定值比较,为什么还是不行,那位帮忙看看
      

  8.   

    out.print(" selected>");
    改为
    out.print(" selected >");试下。