Ajax+JQuery查询用户名是否重复
2016-05-19 00:06
549 查看
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</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" src="js/checkLogin.js"></script>
-->
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" language="javascript" charset="UTF-8">
$(document)
.ready(
function() {
var name = $("#loginName");
name.blur(function() {
var text = name.val();
if (text == "") {
document.getElementById("span").innerHTML = "<p>用户名不能为空</p>";
return;
} else {
$
.post(
"checkLoginName.action?loginName="
+ text,
null,
function(result) {
var obj=eval("("+result+")");//解析JSON格式字符串
//alert(obj["result"]);//获取JSON字符串中的某一元素
//alert(result);
var json=JSON.parse(result);//解析JSON字符串
//alert(json.result);
if (json.result!=1) {
document
.getElementById("span").innerHTML = "<font color='green'>用户名可以用</font>";
} else {
document
.getElementById("span").innerHTML = "<font color='red'>用户名已存在</font>";
}
});
}
});
});
</script>
</head>
<body>
<div align="center" style="width: 100%;height: 100%">
<form action="loginAdmin" name="form1" method="post">
<table align="center">
<tr>
<td>用户名:</td>
<td id="t1"><input type="text" name="admin.loginName"
size="20px" id="loginName" />
<div id="span"></div></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="admin.loginPwd" size="20px" />
</td>
</tr>
<tr>
<td><input type="submit" value="登录" /></td>
<td><input type="button" value="注册" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Action层返回一个JSON格式的字符串,然后JSP页面解析出来就OK了。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</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" src="js/checkLogin.js"></script>
-->
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" language="javascript" charset="UTF-8">
$(document)
.ready(
function() {
var name = $("#loginName");
name.blur(function() {
var text = name.val();
if (text == "") {
document.getElementById("span").innerHTML = "<p>用户名不能为空</p>";
return;
} else {
$
.post(
"checkLoginName.action?loginName="
+ text,
null,
function(result) {
var obj=eval("("+result+")");//解析JSON格式字符串
//alert(obj["result"]);//获取JSON字符串中的某一元素
//alert(result);
var json=JSON.parse(result);//解析JSON字符串
//alert(json.result);
if (json.result!=1) {
document
.getElementById("span").innerHTML = "<font color='green'>用户名可以用</font>";
} else {
document
.getElementById("span").innerHTML = "<font color='red'>用户名已存在</font>";
}
});
}
});
});
</script>
</head>
<body>
<div align="center" style="width: 100%;height: 100%">
<form action="loginAdmin" name="form1" method="post">
<table align="center">
<tr>
<td>用户名:</td>
<td id="t1"><input type="text" name="admin.loginName"
size="20px" id="loginName" />
<div id="span"></div></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="admin.loginPwd" size="20px" />
</td>
</tr>
<tr>
<td><input type="submit" value="登录" /></td>
<td><input type="button" value="注册" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Action层返回一个JSON格式的字符串,然后JSP页面解析出来就OK了。
相关文章推荐
- jquery map调用get()方法的困惑
- jQuery点击输入框显示验证码图片
- jquery实现无刷新验证码的简单实例
- jQuery判断元素是否显示 是否隐藏的简单实现代码
- jquery显示隐藏元素的实现代码
- jQuery获取当前点击的对象元素(实现代码)
- JQuery 在文档中查找指定name的元素并移除的实现方法
- 当jquery ajax遇上401请求的解决方法
- 基于jQuery的Web上传插件Uploadify使用示例
- 浅析jquery数组删除指定元素的方法:grep()
- jquery if条件语句的写法
- jquery遍历table的tr获取td的值实现方法
- 用jQuery获取table中行id和td值的实现代码
- jquery的ajax参数详解
- jquery ajax IE8提示下载文件
- jQuery+Superfish制作下拉菜单
- ssh学习之旅(1)--jQuery获取session的值
- JavaScript、jQuery、Ajax、Json等又是什么?区别又是什么?
- 原生js实现复制对象、扩展对象 类似jquery中的extend()方法
- 那些被漏掉的JQuery总结(三)——关于前台的数据计算问题