如何设置在jsp中选择的多个选项

时间:2013-10-21 08:40:02

标签: jsp servlets

我希望根据数据库结果选择多个选项

<tr>
            <td>Roles :</td>
            <td>
                <select id="role" name="role" multiple>
              <% 
                         while(rs2.next()){
                           String role = rs2.getString("role_name");  
                           if(role.equalsIgnoreCase("role1")){
                           role1= true;
                           }
                           else if(role.equalsIgnoreCase("role2")){
                           role2 = true;
                          }
                           else if(role.equalsIgnoreCase("role3")){
                           role3= true;
                          }
                           else if(role.equalsIgnoreCase("role4")){
                           role4= true;
                          }
                         }
                     %>                                                                                                                               
                    <option value="role1">role1</option>                                          
                    <option value="role2">role2</option>                                                               
                    <option value="role3">role3</option>
                    <option value="role4">role4</option>             

                </select>
            </td>
        </tr>

2 个答案:

答案 0 :(得分:1)

您可以使用selected属性

设置多个选项
 <option value="role1" <%=role1?"selected":""%>>role1</option>                                          
 <option value="role2" <%=role2?"selected":""%>>role2</option>                                                               
 <option value="role3" <%=role3?"selected":""%>>role3</option>
 <option value="role4" <%=role4?"selected":""%>>role4</option>

答案 1 :(得分:0)

你试过这样的吗?

<tr>
        <td>Roles :</td>
        <td>
            <select id="role" name="role" multiple>
          <% 
                     while(rs2.next()){
                       String role = rs2.getString("role_name");  
                       if(role.equalsIgnoreCase("role1")){ %>

                       <option value="role1" selected>role1</option> 

                     <%  role1= true;
                       }else{ %>
                       <option value="role1">role1</option> 
                    <%   }
                       if(role.equalsIgnoreCase("role2")){ %>

                        <option value="role2" selected>role2</option> 

                     <% role2 = true;
                      }else { %>
                        <option value="role2" selected>role2</option> 
                    <%  }
                       if(role.equalsIgnoreCase("role3")){ %>
                       <option value="role3" selected>role3</option> 

                    <%  role3= true;
                      } else { %>
                       <option value="role3">role3</option> 
                    <%  }
                       if(role.equalsIgnoreCase("role4")){ %>
                       <option value="role4" selected>role4</option>
                    <% role4= true;
                      }else{ %>
                      <option value="role4" >role4</option>
                   <% }
                     }
                 %>                                                                                                                               
            </select>
        </td>
    </tr>