求一个使用Ajax的方式实现两级联动的例子。。要全
举个例子,
就比如
分类
男装外套 男装羽绒服
男装马甲
女装外套 女装大衣
女装风衣。
最好把与Ajax相对应的servlet也发上来。。谢谢了。
[解决办法]
这是一个城市两级联动的,应该符合你的要求。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--
北京bj
东城
西城
崇文
天津
和平
河东
河西
-->
<select id="city">
<option value="bj">北京</option>
<option value="tj">天津</option>
</select >
<select id="country"></select>
<script type="text/javascript">
alert("1");
var city = document.getElementById("city");
city.onchange = function() {
alert("2");
//document.getElementById("city").value;
var value = this.value;
var xhr;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject) {
xhr = new ActiveXObject(
"Microsoft.XMLHttp"
);
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200){
//xxx-xxx-xx
var content = xhr.responseText;
var countrys = content.split("-");
var c = document.getElementById("country");
var children = c.childNodes;
while(c.hasChildNodes()) {
c.removeChild(children[0]);
}
for(var i=0;i<countrys.length;i++) {
//alert("xxx");
var option = document.createElement("option");
option.innerHTML = countrys[i];
c.appendChild(option);
}
}
}
}
xhr.open("GET","cityServlet?city=" + value,true);
xhr.send(null);
}
</script>
</body>
</html>
package com.briup.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.corba.se.impl.ior.WireObjectKeyTemplate;
/**
* Servlet implementation class CityServlet
*/
public class CityServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CityServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map<String, List<String>> citys = new HashMap<String, List<String>>();
List<String> bList = new ArrayList<String>();
bList.add("涓滃煄");
bList.add("瑗垮煄");
bList.add("宕囨枃");
citys.put("bj", bList);
List<String> tList = new ArrayList<String>();
tList.add("鍜屽钩");
tList.add("娌充笢");
tList.add("娌宠タ");
citys.put("tj", tList);
request.setCharacterEncoding("UTF-8");
String city = request.getParameter("city");
List<String> list = citys.get(city);
String result = "";
for(String s : list) {
result += s + "-"; ///////
}
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
PrintWriter writer = response.getWriter();
writer.print(result);
}
}
<option value="四川">四川</option>
<option value="天津">天津</option>
<option value="西藏">西藏</option>
<option value="新疆">新疆</option>
<option value="云南">云南</option>
<option value="浙江">浙江</option>
<option value="香港">香港</option>
<option value="澳门">澳门</option>
<option value="台湾">台湾</option>
<option value="其他">其他</option>
</select>
</td>
<td width="20%" align="center">
<input type="button" value="添加" onclick="moveOption(document.myform.list1, document.myform.list2)"><br><br>
<input type="button" value="删除" onclick="moveOption(document.myform.list2, document.myform.list1)">
</td>
<td width="40%">
<select style="width:100%;" multiple name="list2" size="12" ondblclick="moveOption(document.myform.list2, document.myform.list1)">
</select>
</td>
</tr>
</table>
值:<input type="text" name="city" size="40" value="" />
</form>
<script language="JavaScript">
<!--
function moveOption(e1, e2){
try{
for(var i=0;i<e1.options.length;i++){
if(e1.options[i].selected){
var e = e1.options[i];
e2.options.add(new Option(e.text, e.value));
e1.remove(i);
i=i-1
}
}
document.myform.city.value=getvalue(document.myform.list2);
}
catch(e){}
}
function getvalue(geto){
var allvalue = "";
for(var i=0;i<geto.options.length;i++){
allvalue +=geto.options[i].value + ",";
}
return allvalue;
}
//-->
</script>