[英]How to compare two arraylist and set option selected in jsp?
我有兩個表:
表1帶列的geoCountry
country_name | country_code
表2帶有列的user_countries
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>
它在選擇框中顯示重復的值..為什么?
用“ usercountries” 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>
<% }
} %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.