用jquery写的校验用户名
$(function(){ $("input[name='username']").blur(function(){ var uname = $(this).val(); console.log(uname); console.log(uname==""); if (uname=="") { $("#span1").html("用户名不能为空!"); }else{ $.post( "${pageContext.request.contextPath}/userAction_UserExists", {"uname":uname}, function(data){ $("#span1").html(data); }, 'json' ); } }); });
后端java代码:
private String uname; public String UserExists() throws IOException { log.info(uname); if ("admin".equals(uname)) { String s = "[{\"info\":\"用户名不存在!\"}]"; String json = JSONArray.fromObject(s).toString(); ServletActionContext.getResponse().getWriter().write(json); }else { } return null; } public void setUname(String uname) { this.uname = uname; }
只给出了js代码和java代码,html代码太乱了不好意思往上贴