Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

java - JSP: drop down list 2 depends on drop down list 1

I am having difficulties while dealing with two linked drop down lists which the drop down list 1 will fetch the values from the DB and based on the user's selection, it will fetch the concerned records in drop down list 2.

I tried to do that in my jsp with that code, but it did not work and many people advised to use javascript. In fact, I don't know much more abot JS, so can you please help me

<select size="1" name="shop_category"><option value="NONE">  
<%  
             try  
             {  
                            ResultSet rs=null;  
                            Statement st1=null;  
                            String query = "select Category_name, category_id from shop_category_lkup";  
                            st1 = conn1.createStatement();  
                            rs = st1.executeQuery(query);  
                            while(rs.next())                  
            {  
       String sz_Selected="";  
             if (rs.getString("category_id").equals(shop_category))  
             {  
               sz_Selected = "selected";  
             }  
%>  
            <option value="<%=rs.getString("category_id")%>" <%=sz_Selected%>>  
    <%=rs.getString("category_name")%></option>  
<%  
                            }  
            }  
            catch (Exception e) {  
  e.printStackTrace();  
}  
%>  
</select>  

<select size="1" name="rent_category"><option value="NONE">  
<%  
             try  
             {  
                            ResultSet rs=null;  
                            Statement st1=null;  
                            String query = "select r.Category_name, r.category_id from rent_category_lkup r, shop_categpry_lkup s where r.category_id=s.category_id";  
                            st1 = conn1.createStatement();  
                            rs = st1.executeQuery(query);  
                            while(rs.next())                  
            {  
       String sz_Selected="";  
             if (rs.getString("category_id").equals(rent_category))  
             {  
               sz_Selected = "selected";  
             }  
%>  
            <option value="<%=rs.getString("category_id")%>" <%=sz_Selected%>>  
    <%=rs.getString("category_name")%></option>  
<%  
                            }  
            }  
            catch (Exception e) {  
  e.printStackTrace();  
}  
%>  
</select>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're making a fundamental mistake of assuming that the Java code present in the scriptlets gets executed at the client end!

take a look at the lifecycle of a JSP. After that you'll be in a much better position to understand why your code doesn't work.
thereafter, you should try looking into some Cascading Dropdown examples using AJAX.

if all that doesn't help - post in again and it'll be much easier to guide you through.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...