2014-04-14 104 views
-2

這是我的JSP代碼:如何從我的選擇下拉列表中刪除重複的值?

<form method="post" action="" > 
    <table style="width: 100%;border: 0px;border-spacing: 0px;padding: 0px;"> 
    <tr> 

    <td style="height: 28px; font-size: 14px; width:80px; ">Select State :</td> 

    <td style="text-align: left; padding-left: 10px; width: 450px;"> 
     <select name="city" onchange="Consituteshow(this.value)"> 
        <option value="">Andhra Pradesh</option> 
        <% 
              Statement stmt=null; 
              DBconnection db=new DBconnection(); 
              Connection con=db.dbConn(); 
              try{ 
              stmt = con.createStatement(); 
              ResultSet rs = stmt.executeQuery("select distinct StateID,State from election_history;"); 
              while(rs.next()) 
              { 
         %> 

              <option value="<%=rs.getString(2)%>"><%=rs.getString(2)%></option> 

        <%}%> 

     </select> 
    </td> 

    </tr> 

</table> 
</form> 

以上是我的JSP代碼並且在該代碼我給「安德拉邦」作爲默認name.due到這裏面下拉列表「安德拉邦」被印刷twice.Is有任何方法可以刪除它?

+0

你能告訴你的實際的HTML,而不是用來生成HTML腳本? –

+0

那麼你爲什麼不提供「安得拉邦」選項的價值? – Bellash

+0

爲什麼我的問題downvoted ??任何人都可以解釋我? – User2413

回答

2

檢查名稱爲「安得拉邦」,如果它然後選擇與selected="selected"

<!-- no default option here --> 
<option value=""></option> 

    <% 
    while(rs.next()){ 
     if(!rs.getString(2).equals("Andhra Pradesh")){ 
      <option value="<%=rs.getString(2)%>"><%=rs.getString(2)%></option> 
     }else{ 
      <option selected="selected" value="<%=rs.getString(2)%>"><%=rs.getString(2)%></option> 
     } 
    } 
    %> 
+0

==字符串是否相等? –

0

裹一切while語句裏面的選項:

看到這一點:

if(rs.getString(2) !="Andhra Pradesh") { ... }

1

你可以寫查詢

select distinct StateID,State from election_history where State != 'Andhra Pradesh' 

,或者您可能需要使用jQuery.unique()方法

0
<select name="city" onchange="Consituteshow(this.value)"> 
       <% 
    Statement stmt=null; 
    DBconnection db=new DBconnection(); 
    Connection con=db.dbConn(); 
    try{ 
    stmt = con.createStatement(); 
    ResultSet rs = stmt.executeQuery("select distinct StateID,State from election_history;"); 
    while(rs.next()){ 
       if(!rs.getString(2).equals("Andhra Pradesh")){ 
        %> 
      <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option> 
       <%}else{%> 
<option selected='selected' value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option> 
    <%} 
}%></select> 
相關問題