t1.jsp
t2.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>联动的下拉菜单</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#wc").change(function(){
$.getJSON("t2.jsp",{index: $(this).val()}, function(myJSON){
var myOptions = '';
for (var i = 0; i < myJSON.length; i++) {
myOptions += '<option value="' + myJSON[i].optionValue + '">' + myJSON[i].optionValue + '</option>'; }
$("#gx").html(myOptions);
});
});
$("#wc").change();
})
</script>
</head>
<body>
<select name="wc" id="wc">
<option>一</option>
<option>二</option>
<option>三</option>
</select>
<select name="gx" id="gx">
</select>
</body>
</html>t2.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%
String index = new String(request.getParameter("index").getBytes("iso8859-1"),"utf-8");
String JSON_text = "";
if(index.compareTo("一")==0)
JSON_text ="[{optionValue:'一一'},{optionValue:'一二'}]";
else if(index.compareTo("二")==0)
JSON_text ="[{optionValue:'二一'},{optionValue:'二二'}]";
else if(index.compareTo("三")==0)
JSON_text ="[{optionValue:'三一'},{optionValue:'三二'}]";
else
JSON_text ="[{optionValue:'"+index+"'}]";
out.println(JSON_text);
%>
本文介绍了一种使用jQuery实现的联动下拉菜单效果。当用户选择第一个下拉菜单中的选项时,第二个下拉菜单会根据所选选项动态更新其内容。此功能通过发送AJAX请求获取JSON数据并填充到第二个下拉菜单中来实现。
6689

被折叠的 条评论
为什么被折叠?



