此小程序的功能主要是采用异步请求方式从数据库中调取省市区信息显示到下拉列表:
代码如下:
建立数据库中的代码和一些配置文件信息就省略了,主要有JavaScript中的代码为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
$(document).ready(function(){ $.get( "getProvince.do" , function(result){ $( "#showp" ).html(result); }); }) var xmlhttp; function mysend(str){ $(document).ready(function(){ $( "#show2" ).html( "" ); }) var show = document.getElementByIdx_x_x_x_x_x_x_x_x_x( "show" ); show.innerHTML = "" ; var province = document.getElementByIdx_x_x_x_x_x_x_x_x_x( "province" ).value; if (province!= 0 ){ if (window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" ); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState== 4 && xmlhttp.status== 200 ){ show.innerHTML = xmlhttp.responseText; } } var ss = encodeURIComponent(str); xmlhttp.open( "GET" , "getCity.do?provinceid=" +ss, true ); xmlhttp.send( null ); } } function myarea(str){ if (window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" ); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState== 4 && xmlhttp.status== 200 ){ var show2 = document.getElementByIdx_x_x_x_x_x_x_x_x_x( "show2" ); show2.innerHTML = xmlhttp.responseText; } } var ss = encodeURIComponent(str); xmlhttp.open( "GET" , "getArea.do?cityid=" +ss, true ); xmlhttp.send( null ); } |
html页面中的代码为:
所在地
action中的代码为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
package mobi.zhangsheng.jiejia.action; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.springframework.stereotype.Controller; import mobi.zhangsheng.jiejia.domain.Areas; import mobi.zhangsheng.jiejia.service.AgentsService; import mobi.zhangsheng.jiejia.service.AreasService; @Controller public class ProvinceAction { private int provinceid; private int cityid; @Resource private AreasService as; @Resource private AgentsService ags; public int getProvinceid() { return provinceid; } public void setProvinceid( int provinceid) { this .provinceid = provinceid; } public int getCityid() { return cityid; } public void setCityid( int cityid) { this .cityid = cityid; } public void getProvince(){ List provinceList = as.getAreasPrvinceList(); HttpServletResponse resp= ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); //resp.setContentType("xml"); resp.setContentType( "text/html" ); resp.setCharacterEncoding( "utf-8" ); try { PrintWriter out = resp.getWriter(); out.print( "<img id=" theimg " onclick=" window.open( this .src) " src=" //files.jb51.net/file_images/article/201601/2016113145609190.png?2016013145623" id="codetool">
主要的功能代码都在上面了,如有不懂的请联系QQ:1037139984,祝大家都有好的发展,共同学习,共同成长。 延伸 · 阅读
精彩推荐
|