如何从我的选择下拉列表中删除重复值?

时间:2014-04-14 09:58:35

标签: javascript jquery jsp html.dropdownlistfor

这是我的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代码,在这段代码中我给了'Andhra Pradesh'默认name.due到这个内部下拉列表'Andhra Pradesh'打印两次。有什么方法可以删除它???

4 个答案:

答案 0 :(得分:2)

检查姓名是否为&#34; Andhra Pradesh&#34;如果是,则选择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>  
        }
    }
    %>

答案 1 :(得分:1)

你可以写查询

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

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

答案 2 :(得分:0)

将所有内容包含在while语句中:

看到这个:

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

答案 3 :(得分: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>