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教程之由省份选择城市。