AJAX异步刷新-----判断用户名是否可用,预览头像

前端之家收集整理的这篇文章主要介绍了AJAX异步刷新-----判断用户名是否可用,预览头像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. 	String path = request.getContextPath();
  4. 	String basePath = request.getScheme() + "://"
  5. 			+ request.getServerName() + ":" + request.getServerPort()
  6. 			+ path + "/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'Region.jsp' starting page</title>
  13. <Meta http-equiv="pragma" content="no-cache">
  14. <Meta http-equiv="cache-control" content="no-cache">
  15. <Meta http-equiv="expires" content="0">
  16. <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <Meta http-equiv="description" content="This is my page">
  18. <!--
  19. 	<link rel="stylesheet" type="text/css" href="styles.css">
  20. 	-->
  21. <script type="text/javascript" src="js/my.js"></script>
  22. <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
  23. <script type="text/javascript" src="js/address.js"></script>
  24. <script type="text/javascript" src="js/China.js"></script>
  25. <script type="text/javascript">
  26. 	//如果两次密码 不相同那么就会返回false  如果false就不会提交
  27. 	function count() {
  28. 		var one = $("#onepas").val();
  29. 		var two = $("#twopas").val();
  30. 		var name = $("#username").val();
  31. 		var namefont = $("#namefont").val();
  32. 		if (name == "") {
  33. 			alert("用户名不能为空");
  34. 			return false;
  35. 		} else if (namefont == "用户名已存在") {
  36. 			alert("用户名已存在");
  37. 			return false;
  38. 		} else if (namefont == "用户名不能包含中文") {
  39. 			alert("用户名不能包含中文");
  40. 			return false;
  41. 		} else if (one == "" || two == "") {
  42. 			alert("密码或者确认密码不能为空");
  43. 			return false;
  44. 		} else if ($("#yzmtext").val() == "") {
  45. 			alert("验证码不能为空");
  46. 			return false;
  47. 		} else if ($("#yzmfont").text() == "验证码输入错误") {
  48. 			alert("验证码输入错误");
  49. 			return false;
  50. 		} else if (one == two) {
  51. 			$("#warning").html() = "";
  52. 			return true;
  53. 		} else {
  54. 			$("#warning").html("两次密码输入不一致");
  55. 			alert("两次密码输入不一致");
  56. 			return false;
  57. 		}
  58. 	}
  59. 	//动态判断用户名是否可用
  60. 	function nameChange() {
  61. 		//获取xmlhttprequest对象
  62. 		var xmlhttp = getXMLHttpRequest();
  63. 		xmlhttp.onreadystatechange = function() {
  64. 			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  65. 				//做任意事情
  66. 				//获取到相应的内容
  67. 				var namefont = $("#namefont");
  68. 				var str = xmlhttp.responseText;
  69. 				if (str == 1) {
  70. 					namefont.css("color","red");
  71. 					namefont.html("用户名已存在");
  72. 				} else if (str == 2) {
  73. 					namefont.css("color","red");
  74. 					namefont.html("用户名不能包含中文");
  75. 				} else {
  76. 					namefont.css("color","green");
  77. 					namefont.html("用户名可用");
  78. 				}
  79. 			}
  80. 		};
  81. 		//定义访问的网址
  82. 		var url = "${pageContext.request.contextPath}/servlet/judgeUserNameServlet?username="
  83. 				+ $("#username").val();
  84. 		xmlhttp.open("get",url);
  85. 		//发送请求
  86. 		xmlhttp.send();
  87. 	}
  88. 	//刷新验证码
  89. 	var i = 0;
  90. 	function noSee() {
  91. 		$("#yzm").attr("src","${pageContext.request.contextPath}/servlet/YZM?id=" + i);
  92. 		//document.getElementById("yzm").src=
  93. 		i++;
  94. 	}
  95. 	//动态判断验证码是否正确
  96. 	function yzmChange() {
  97. 		//获取xmlhttprequest对象
  98. 		var xmlhttp = getXMLHttpRequest();
  99. 		xmlhttp.onreadystatechange = function() {
  100. 			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  101. 				var str = xmlhttp.responseText;
  102. 				//var msg=document.getElementById("yzmfont");
  103. 				if (str == 0) {
  104. 					$("#yzmfont").css("color","red");
  105. 					$("#yzmfont").html("验证码输入错误");
  106. 				} else {
  107. 					$("#yzmfont").css("color","green");
  108. 					$("#yzmfont").html("验证码正确");
  109. 				}
  110. 			}
  111. 		};
  112. 		//定义访问的网址
  113. 		var url = "${pageContext.request.contextPath}/servlet/JudgeCode?yzm="
  114. 				+ $("#yzmtext").val();
  115. 		xmlhttp.open("get",url);
  116. 		//发送请求
  117. 		xmlhttp.send();
  118. 	}
  119. 	//图片预览
  120. 	function fileChange() {
  121. 		//获取xmlhttp
  122. 		var xmlhttp = getXMLHttpRequest();
  123. 		//获取工程路径
  124. 		var conPath = "${pageContext.request.contextPath}";
  125. 		xmlhttp.onreadystatechange = function() {
  126. 			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  127. 				var str = xmlhttp.responseText;
  128. 				//alert(conPath+str);
  129. 				$("#icon").val(str);
  130. 				$("#img").attr("src",conPath + str);
  131. 			}
  132. 		};
  133. 		var file = document.getElementById("file");
  134. 		xmlhttp.open("post",file.action);
  135. 		xmlhttp.send(new FormData(file));
  136. 	}
  137. 	function adre() {
  138. 		$("#addressAll").val("");
  139. 	}
  140. </script>
  141. <script type="text/javascript">
  142. 	window.onload = function() {
  143. 		//默认省
  144. 		$("#provice").html(getProBegin());
  145. 		//显示默认的市
  146. 		$("#city").html(getCityBegin(0));
  147. 		//显示默认的区
  148. 		$("#area").html(getAreaBegin(0,0));
  149. 	}
  150. 	//当省改变时   自动改变该省的市
  151. 	function changePro() {
  152. 		//当前的省的value
  153. 		var proValue = $("#provice").val();
  154. 		//获取返回的更新后的市,区列表
  155. 		var html = onProChange(proValue);
  156. 		$("#city").html(html[0]);
  157. 		$("#area").html(html[1]);
  158. 	}
  159. 	//当市改变时  区自动变为该市的区
  160. 	function changeCity() {
  161. 		//获取当前的市的value
  162. 		var cityValue = $("#city").val();
  163. 		$("#area").html(onCityChange(cityValue));
  164. 	}
  165. </script>
  166. <style type="text/css">
  167. #ns {
  168. 	position: relative;
  169. 	top: 500px;
  170. 	left: 520px;
  171. }
  172. #yzm {
  173. 	position: absolute;
  174. 	top: 380px;
  175. 	left: 260px;
  176. }
  177. #ns {
  178. 	position: absolute;
  179. 	top: 430px;
  180. 	left: 260px;
  181. }
  182. #regPage {
  183. 	position: absolute;
  184. 	top: 2px;
  185. 	left: 500px;
  186. }
  187. #attention {
  188. 	position: absolute;
  189. 	top: 60px;
  190. 	left: 240px;
  191. }
  192. #img {
  193. 	position: absolute;
  194. 	top: -200px;
  195. 	left: 400px;
  196. }
  197. #submit {
  198. 	position: relative;
  199. 	top: 70px;
  200. }
  201. #addresdiv {
  202. 	position: relative;
  203. 	top-10px;
  204. }
  205. #file {
  206. 	position: relative;
  207. 	top: -80px;
  208. }
  209. #yzmdiv {
  210. 	position: relative;
  211. 	top: 50px;
  212. }
  213. #addfont {
  214. 	position: relative;
  215. 	top: -60px;
  216. }
  217. </style>
  218. </head>
  219. <body>
  220. 	<h1 id="regPage" color="red">欢迎注册</h1>
  221. 	<br>
  222. 	<br>
  223. 	<br>
  224. 	<!--  <form action="${pageContext.request.contextPath }/servlet/RegionServlet" enctype="multipart/form-data" method="post" > -->
  225. 	<form
  226. 		action="${pageContext.request.contextPath }/servlet/RegionServlet"
  227. 		method="post">
  228. 		用户名<input type="text" id="username" name="username"
  229. 			onchange="nameChange()" /> <font id="attention" color="red">用户名只能有数字,字母,下划线组成</font><br>
  230. 		<font id="namefont"></font><br> <br> 密码:<input
  231. 			type="password" id="onepas" name="password" /><br> <br>
  232. 		确认密码:<input type="password" id="twopas" name="repassword" /> <font
  233. 			id="warning" color="red"></font><br> <br> <input id="icon"
  234. 			type="hidden" name="icon" value="">
  235. 		<div id="addressdiv">
  236. 			选择省:<select id="provice" name="pro" onchange="changePro()"></select>
  237. 			选择市:<select id="city" name="city" onchange="changeCity()"></select>
  238. 			选择区:<select id="area" name="area"></select><br> <br> <font
  239. 				id="addfont"> 详细地址:</font>
  240. 			<textarea id="addressAll" name="addressAll" rows=5 cols=35
  241. 				onclick="adre()" onclick="">乡/镇/街道</textarea>
  242. 			<br> <br>
  243. 		</div>
  244. 		<div id="yzmdiv">
  245. 			验证码:<input type="text" id="yzmtext" name="yzm" onchange="yzmChange()" /><br>
  246. 			<font id="yzmfont"></font><br> <br>
  247. 		</div>
  248. 		<input type="submit" id="submit" value="提交" onclick="return count()" />
  249. 	</form>
  250. 	<form id="file"
  251. 		action="${pageContext.request.contextPath}/servlet/ImageFileServlet"
  252. 		enctype="multipart/form-data" method="post">
  253. 		头像:<input type="file" name="photo" onchange="fileChange()" /> <img
  254. 			id="img" width="300px" height="300px" src="img/moren.png">
  255. 	</form>
  256. 	<img id="yzm" onclick="noSee()"
  257. 		src="${pageContext.request.contextPath }/servlet/YZM">
  258. 	<font id="ns" onclick="noSee()" color="red">看不清楚点我</font>
  259. 	<br>
  260. 	<font color="green">登录请点击这里</font>
  261. 	<br>
  262. 	<button>
  263. 		<a href="${pageContext.request.contextPath}/Login.jsp">登录</a>
  264. 	</button>
  265. </body>
  266. </html>


  267.  

猜你在找的Ajax相关文章