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")){
                           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>
4

2 回答 2

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>
于 2013-10-21T09:23:27.513 回答
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>
于 2013-10-21T09:36:57.440 回答