0

我有两张桌子:

geoCountry带列的表 1

country_name | country_code

user_countries带列的表 2

country_name | username 

我想设置用户国家预选(即从数据库中获取)

这是我尝试过的代码

     <select multiple="multiple" class="country" style="width: 200px">
         <%
           sql6 = "SELECT DISTINCT countryname FROM user_countries WHERE username = ?";
           ps6 = connection.prepareStatement(sql6);
           ps6.setString(1, user);
           rs6 = ps6.executeQuery();

            while (rs6.next()) {
                usercountries.add(rs6.getString(1));
             }

            for (int i = 0; i < geoCountry.size(); i++) {
                 for (int j = 0; j < usercountries.size(); j++) {
                    if (usercountries.get(j).equals(geoCountry.get(i))) {
                 %>
                <option value="<%=geoCountry.get(i)%>" selected="selected"><%=geoCountry.get(i)%></option>
                 <% } else {%>
                 <option value="<%=geoCountry.get(i)%>"><%=geoCountry.get(i)%></option>
              <% }
              }
           } %>
      </select>

它在选择框中显示重复的值..为什么?

4

1 回答 1

0

删除带有“用户国家/地区”的第二个周期,thms。喜欢:

  for (int i = 0; i < geoCountry.size(); i++) {
         if (usercountries.contains(geoCountry.get(i)) {
             %>
            <option value="<%=geoCountry.get(i)%>" selected="selected"><%=geoCountry.get(i)%></option>
             <% } else {%>
             <option value="<%=geoCountry.get(i)%>"><%=geoCountry.get(i)%></option>
          <% }
       } %>
于 2013-11-08T10:32:59.237 回答