我将描述两种方法:使用/不使用AJAX.
>如果要进行同步表单提交,则需要将onchange事件附加到第一个选择元素:
1
2
3
以这种方式完成后,表单将被提交,第一个选择选项将作为request.getParameter(“select-one”)提供,根据该选项,您将为第二个下拉填充提供数据,通常转发到JSP.
>如果要通过AJAX检索列表并重新填充另一个下拉列表,可以发送AJAX请求并处理回调函数中的返回数据:
var val = $('#select-one option:selected').val();
$.ajax({
url: "servletURL",//servlet URL that gets first option as parameter and returns JSON of to-be-populated options
type: "POST",//request type, can be GET
cache: false,//do not cache returned data
data: {one : val},//data to be sent to the server
dataType: "json"//type of data returned
}).done(function(data) {
var second = $("#select-two");
$.each(data, function() {
options.append($("").val(this.value).text(this.label));
});
});
以这种方式完成后,将重新填充第二个下拉列表而不刷新页面.