简体   繁体   English

如何使用jsp根据另一个下拉列表在下拉列表中显示值?

[英]how to display value in dropdown list depending on another dropdownlist using jsp?

I'm tying to bind the value to dropdownlist ie named as subtype which is depending on another dropdownlist ie named as type .我想将值绑定到 dropdownlist ,即命名为subtype ,这取决于另一个 dropdownlist ,即命名为type Please tell me how I can do that.. I have given I following code which implement for binding the dropdownlist depending on another dropdownlist.请告诉我如何做到这一点.. 我已经给了我以下代码,这些代码实现了根据另一个下拉列表绑定下拉列表。

  `<table>
  <tr>
  <td>
 <label id="type">Chosen category : </label>
 </td>
 <td>
 <% String getType=request.getParameter("id");
 out.println("<h4>"+getType+"  <a href='post free ads.jsp'>Change the
 category</a>   </h4>");
 %>
 </td>
 </tr>

 <tr>
 <td>
 <label id="typeselect">Select type : </label>
 </td>
 <td>
 <select name="type">  
     <option value="none">Select</option>  
  <%
  Connection conn = NewClass.getOracleConnection();
  Statement stmt = conn.createStatement();  
  ResultSet rs = stmt.executeQuery("Select distinct company from admin.all_stuff
   where stuff_type='"+getType+"' ");
  while(rs.next()){
      %>
      <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
       <%
  }
 %>
  </select>
  </td>
 </tr>
 <tr>
 <td> <label id="subtypeselect">Select Subtype : </label></td>
 <td>
     <select name="subtype">  
  <option value="none">Select</option>  
     <%
  try
  { %>
 <% String typeselected = request.getParameter("type");
  Statement stmtmt = conn.createStatement();  
  ResultSet rse = stmtmt.executeQuery("Select model_no from admin.all_stuff
  where stuff_type='"+getType+"' and company='"+typeselected+"' ");
  while(rse.next()){
      %>
      <option value="<%=rse.getString(1)%>"><%=rse.getString(1)%></option> 
      <%
  }
  %>
  <% }
  catch(Exception e)
  {
      out.println(e.getMessage());
  }
  %>
  }
    </select> 
     
 </td>
  </tr>
    </table>`

You need AJAX for this to get it work.你需要 AJAX 来让它工作。

Your jsp is compiled into a servlet and then delivered to your client.您的 jsp 被编译成一个 servlet,然后交付给您的客户端。 After that no more changes are possible.之后就无法再进行更改。

You have to handle an onChange event on the first drop down box in HTML and then post an AJAX request to your server to send back the content for the second drop down box.您必须在 HTML 中的第一个下拉框中处理onChange事件,然后将 AJAX 请求发布到您的服务器以发回第二个下拉框的内容。

Easiest way to start with AJAX is to get some library for example jQuery开始使用 AJAX 的最简单方法是获取一些库,例如 jQuery

$.get('ajax/test.html', function(data) {
    alert(data);
});

This code allow us to download from HTML page some content from URL 'ajax/test.html'.此代码允许我们从 URL 'ajax/test.html' 从 HTML 页面下载一些内容。 Second argument is callback function.第二个参数是回调函数。 Variable data has content of page 'ajax/test.html'变量数据具有页面“ajax/test.html”的内容

More: http://api.jquery.com/jQuery.get/更多: http : //api.jquery.com/jQuery.get/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从jsp的下拉列表中获取价值? - How to get value from dropdown list in jsp? 如何使用struts2和mysql在下拉列表中的jsp页面上显示从数据库中获取的数据 - How display data fetched from database on a jsp page in dropdown list using struts2 with mysql 如何使用jsp:useBean和jsp:setProperty获取下拉值? - How to get dropdown value using jsp:useBean and jsp:setProperty? 如何在弹簧中将下拉选择的值从一个jsp传递到另一个 - how to pass dropdown selected value from one jsp to another in springs 在JSP中获取下拉列表的值,并根据选择的值加载其他内容 - Get value of dropdown in JSP and load other contents depending on the value selected 如果值是另一个类的成员,则从JSP的下拉列表中获取值 - To get value from dropdown list in JSP in case the value is a member of another class Netbeans根据另一个dropdownlist html netbeans填充下拉列表 - Netbeans populating a dropdown list based on another dropdownlist html netbeans 如何在.JSP文件中显示列表? - How to display a list in a .JSP file? 使用JSP从下拉列表传递所选值 - Pass selected value from dropdown using JSP 当第一个下拉菜单在jsp中使用spring标签选择一个值时,需要另一个下拉菜单 - Need another dropdown menu when first drop down selects a value using spring tag in jsp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM