[英]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.