JQ完成省市联动(JSON)

1.引入的包


2.代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>注册页面</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
		-->

<script type="text/javascript">
	function checkusername() {
		var username = document.getElementById("username").value;
		//创建异步对象
		var xh = new XMLHttpRequest();
		//设置状态改变的监听
		xh.onreadystatechange = function() {
			if (xh.readyState == 4) {//请求发送成功
				if (xh.status == 200) {//响应成功
					var data = xh.responseText;
					if (data == 1) {
						document.getElementById("s1").innerHTML = "用户名可以使用";
						document.getElementById("sub").disabled = false;
					} else if (data == 2) {
						document.getElementById("s1").innerHTML = "用户名已经被注册了";
						document.getElementById("sub").disabled = true;
					}

				}
			}
		}
		xh.open("GET", "/J2EE/usernamecheckservlet?username=" + username, true);
		xh.send(null);
	}
</script>
<script type="text/javascript" src="/J2EE/html/jquery-3.2.1.js"></script>
<script type="text/javascript">
	$(function() {
		$("#username").blur(function() {
			var username = $("#username").val();
			//演示get/post方法$.get(處理的servlet,传给servlet的map,回调函数(servlet:response.getWriter().println(1)的值))
			$.get("/J2EE/usernamecheckservlet", {
				"username" : username
			}, function(data) {
				if (data == 1) {
					$("#s1").html("用户名可以使用");
					$("#sub").attr("disabled", false);//改标签的属性值
				} else if (data == 2) {
					$("#s1").html("用户名已经被注册了");
					$("#sub").attr("disabled", true);
				}
			});
		});
	});
</script>
<!-- <script type="text/javascript">
	$(function() {
		//为省份的下拉列表绑定事件
		$("#pro").change(
				function() {
					var pid = $(this).val();
					$.post("/J2EE/searchcity", {
						"pid" : pid
					}, function(data) {
						$(data).find("city").each(
								function() {/* 通过find方法查找所以的city元素,并通过each进行遍历 */
									var cid = $(this).children("cid").text();/* 当前city的子元素cid的文本内容 */
									var cname = $(this).children("cname")
											.text();
									$("#city").append(
											"<option value='"+cid+"'>" + cname
													+ "</option>");
								});
					});
				});
	});
</script> -->
<script type="text/javascript">
	$(function() {
		//为省份的下拉列表绑定事件
		$("#pro").change(
				function() {
					var pid = $(this).val();
					$.post("/J2EE/jasonservlet", {
						"pid" : pid
					}, function(data) {
						var $city = $("#city");
						$city.html("<option>--请选择--</option>");//清空
						$(data).each(
								function(i, n) {
									$city.append("<option value='"+n.cid+"'>"
											+ n.cname + "</option>");
								});

					}, "json");//JS识别json
				});
	});
</script>
<style type="text/css" contenteditable="inherit">
div {
	width: 100%;
}

#bodydiv {
	width: 90% px;
}

.logodiv {
	width: 33%;
	float: left;
	height: 48px;
}

.clear {
	clear: both;
}

.cd {
	background-color: black;
	height: 30px;
}
</style>


</head>

<body>
	<div id="bodydiv">
		<div class="logodiv">
			<img alt="" src="/J2EE/imag/head1.png" height=100%>
		</div>
		<div class="logodiv">
			<img alt="" src="/J2EE/imag/head2.png" height=100%>
		</div>
		<div class="logodiv"
			style="padding-top: 5px; text-align: center; height: 43px"
			align="center">
			<a href="#">登陆</a> <a href="zhuce.html">注册</a> <a href="#">购物车</a>
		</div>
		<div class="clear"></div>
		<div class="cd">
			<a href="shouyediv.html">首页</a> <a href="#">手机数码</a> <a href="#">电脑办公</a>
			<a href="#">研烟酒箱包</a>
		</div>
		<div
			style="height: 800px; background-image: url('/J2EE/imag/zhuce.jpg'); background-size: 100% 100%;">
			<div style=" height:90%;position: absolute;top: 300px;" align="left">
				<form method="post" action="/J2EE/registservlet">
					<table align="center" border="0" width=40%>
						<tr>
							<td align="right" width=50%>用户名    
							<td align="left"><input class="required" type="text"
								name="username" id="username"></input><span id="s1"></span>
							</td>
						</tr>
						<tr>
							<td align="right" width=50%>密码    
							<td align="left"><input class="required" type="password"
								name="password" id="password"></input>
						</tr>
						<tr>
							<td align="right" width=50%>确认密码    
							<td align="left"><input class="required" type="password"
								name="repassword"></input><span id="repassword"></span></td>
						</tr>
						<tr>
							<td align="right" width=50%>邮箱    
							<td align="left"><input type="text" name="email"></input>
							</td>
						</tr>
						<tr>
							<td align="right" width=50%>性别    
							<td align="left"><input type="radio" name="sex" value="man"
								checked="checked">男<input type="radio" name="sex"
								value="woman">女</td>
						</tr>
						<tr>
							<td align="right" width=50%>生日    
							<td align="left"><input type="date" name="birth">
							</td>
						</tr>
						<tr>
							<td align="right" width=50%>爱好    
							<td align="left"><input type="checkbox" name="love"
								value="1">看电视<input type="checkbox" name="love"
								value="2">打游戏<input type="checkbox" name="love"
								value="3">吃饭<input type="checkbox" name="love" value="4"
								checked>睡觉</td>
						</tr>
						<tr>
							<td align="right" width=50%>籍贯    
							<td align="left"><select id="pro" name="pro">
									<option>--请选择--</option>
									<c:forEach var="p" items="${province}">
										<option value="${p.pid}">${p.pname}</option>
									</c:forEach>

							</select> <select name="city" id="city">
									<option>--请选择--
							</select>
						<tr>
							<td align="right" width=50%>头像    
							<td align="left"><input type="file">
							</td>
						</tr>
						<tr>
							<td align="right" width=50%>个人简介    
							<td align="left"><textarea cols="" rows="5" name="a">我是一个</textarea>
							</td>
						</tr>
						<tr>
							<td colspan="2" align="center"><input type="submit"
								value="注册  " id="sub"><input type="reset" value="重置">
							</td>
						</tr>

					</table>
				</form>
			</div>
		</div>
		<div>
			<img alt="" src="../imag/q.png" width="100%">
		</div>
		<div style="text-align: center;">
			<a href="html.html">关于我们</a>  公司简介  招贤纳士  广告服务  联系方式  合作伙伴  论坛反馈<br>版权声明:Copyright
			© 1999-2016,赵瑾的网站 , All Rights Reserved
		</div>
	</div>
</body>
</html>

package regist;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

public class jasonservlet extends HttpServlet {
	/*
	 * 相应json的JQ案例;跳转到注册页面
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pid=request.getParameter("pid");
		provinceservice ps=new provinceservice();
		try {
			List<city>cities=ps.searchcity(pid);
			//不想要的属性删除
			JsonConfig jc=new JsonConfig();
			jc.setExcludes(new String[]{"pid"});
			//将list集合转为json

			JSONArray ja = JSONArray.fromObject(cities,jc);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().println(ja.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值