標籤:function 使用者名稱 change
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title><script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.1.min.js"></script><script type="text/javascript">$(function(){$(":input[name=‘username‘]").change(function(){var val = $(this).val();val = $.trim(val);if(val != ""){var url = "${pageContext.request.contextPath}/validatUsername";var args = {"username":val, "time":new Date()};$.post(url, args, function(data){$("#message").html(data);});}});})</script></head><body><!-- 1.匯入JQuery庫2.擷取name="username"的結點:username3.1為username添加change響應函數。去除空格且不為空白。準備發生Ajax請求。3.2發送Ajax請求檢驗username是否可用3.3在服務端返回一個html片段3.4在用戶端瀏覽器把其直接添加到#message的html中 --><form action="" method="post">Username<input type="text" name="username"/><br/><div id="message"></div><br/><input type="submit" value="submit"/></form></body></html>
servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubList<String> usernames = Arrays.asList("aaa","bbb","ccc");String username = request.getParameter("username");String result= null;if (usernames.contains(username)) {result = "<font color=‘red‘>該使用者名稱已經被使用</font>";}else{result = "<font color=‘red‘>該使用者名稱可以使用</font>";}response.setContentType("text/html;charset=UTF-8");response.setCharacterEncoding("UTF-8");response.getWriter().print(result);}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4B/F6/wKiom1Q2N6DAbcXoAAA3LBrlRkM507.jpg" title="QQ20141009152149.png" alt="wKiom1Q2N6DAbcXoAAA3LBrlRkM507.jpg" />
本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1561787
Ajax學習筆記-驗證使用者名稱是否可用