jsp使用jquery+ajax实现二级联动下拉菜单

t1.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);
%>

你可能感兴趣的:(html,jquery,jsp,Ajax,json)