$(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代碼:html
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代碼太亂了很差意思往上貼java