AJAX教程之由省份選擇城市_AJAX教程
推薦:xmlHttpRequest實踐之無刷新驗證用戶名現在好多網站上的注冊都用了無刷新驗證用戶名,這種效果咋看感覺很復雜很難實現,其實它里面用到了Ajax中的核心xmlHttpRequest這個類,如果只是單單想實現這個效果,壓根就不用引用Ajax.Net中的組件,因為感覺有點大材小用,下面是具體實現這種效果的方法,希望能給
一、test.html
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
</head>
<script type="text/javascript">
function getResult(stateVal) {
var url = "servlet/SelectCityServlet?state="+stateVal;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
var city = req.responseXML.getElementsByTagName("city");
//alert(city.length);
var str=new Array();
for(var i=0;i<city.length;i++){
str[i]=city[i].firstChild.data;
}
//alert(document.getElementById("city"));
buildSelect(str,document.getElementById("city"));
}
}
}
function buildSelect(str,sel) {
sel.options.length=0;
for(var i=0;i<str.length;i++) {
sel.options[sel.options.length]=new Option(str[i],str[i])
}
}
function test(){
//alert("test");
}
</script>
<body>
<select name="state" onChange="getResult(this.value)">
<option value="">請選擇</option>>
<option value="zj">浙江</option>>
<option value="zs">江蘇</option>>
</select>
<select id="city"></select>
</body>
</html>
二、servlet源程序
package com.stephen.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import org.dom4j.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author stephen
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class SelectCityServlet extends HttpServlet {
public SelectCityServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF8");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
String state = request.getParameter("state");
Document document = DocumentHelper.createDocument();
Element root = document.addElement("state");
Element city = null;
if("zj".equals(state)){
city = root.addElement("city");
city.setText("杭州");
city = root.addElement("city");
city.setText("huzhou");
}else{
city = root.addElement("city");
city.setText("南京");
city = root.addElement("city");
city.setText("蘇州");
city = root.addElement("city");
city.setText("yangzhou");
}
PrintWriter out=response.getWriter();
String s = root.asXML();
out.write(s);
out.close();
}
}
分享:jPager一個適合Ajax+JSON+jQuery環境使用的多功能頁碼欄事由:由于最近在測試開發的一個ASP.NET MVC的項目需要用到頁碼欄(并且需要用到AJAX+JSON傳輸數據),而微軟發布的.NET3.5 CTP 的MVCTOOLKIT里面又沒有提供,網上找了下似乎也沒有太稱心的,于是就自己動手做一個。 由于這個項目用到頁碼欄的地方大多是后臺
- 相關鏈接:
- 教程說明:
AJAX教程-AJAX教程之由省份選擇城市
。