JS:
<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script> <script type="text/javascript"> $(document).ready( function(){ $("#uname").blur( //添加文本框失去焦点事件 function(){ var uname=$("#uname").val(); //获取文本框的值 $.ajax({ //获取 ajax type:‘get‘, //请求方式 url:‘<%=basePath%>login.action‘, //发送给哪个Action data:"uname="+uname, //发送uname给Action success:function(data){ //执行成功后的函数 data参数有Action返回自动赋值 if(data=="success") //如果返回值为success { $("#nameMessage").html("验证成功"); //在span标签显示验证成功 }else{ $("#nameMessage").html("验证失败"); } } }); }) }); </script>
JSP:
<body> <form action="<%=basePath%>login.action"> <p> <input type="text" name="uname" id="uname"> </p> <p> <input type="text" name="upwd" id="upwd"> </p> <span id="nameMessage"></span> </form> </body>
Action:
public class LoginAction extends ActionSupport { private String uname; private String upwd; @Override public String execute() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); if ("lwq".equals(uname)) { out.print("error"); } else { out.print("success"); //最后由ajax中data自动接收 } out.close(); return null; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; }
原文地址:https://www.cnblogs.com/MonkeyJava/p/10809771.html
时间: 2024-10-11 14:19:49