现在有个需求就是异步验证用户名是否存在。用的技术是jquery异步验证和struts2(其实springmvc也是一样的道理,都只是一个控制器).
表单:
value="${dept.dept_name}" onchange="tocheckname()"/>
js代码:
function tocheckname()
{
var deptName= $("#dept_name").val();
$.ajax({
type:"POST",
cache:false,
url : "${rootPath}/dept/checkdeptname.htm",
dataType : "text",
data:{"dept.id":"${dept.id}","dept.dept_name":deptName},
async:false,
success : function(data){
if(0
$("#dept_name").attr("value","");
$("#dept_name").focus();
}
}
});
}
后台的代码:
jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
/**
* 根据科室名称查询是否存在此科室
*
* @return
* @throws Exception
*/
public String checkdeptname() throws Exception {
if (UtilAPI.isNull(dept)) {
dept = new Dept();
}
int count = this.deptService.checkdeptname(dept.getId(), dept.getDept_name()); //如果存在返回1,不存在返回0
try {
response.getOutputStream().print(count);
}
catch (IOException e) {
e.printStackTrace();
}
return Action.NONE;
}










