jquery焦点获取失败
$("#username").focus();
失败的原因: 由于使用了jquery,所以返回的是一个jquery对象,而不是DOM
修改为: 1, $("#username")[0].focus();
2 增加定时器来执行获取焦点
使用jquery+ajax实现账号注册时的账号验证
<script type="text/javascript"> //页面加载时,自动获取焦点 $(document).ready(function(){ setTimeout(function(){ $("#username").focus(); },50) }) //ajax判断账号 $(function(){ $("#username").blur(function(){var uname = $(this).val(); if(uname==""){ $("#msg").css("color","red").html("用户名不能为空"); $("#username")[0].focus(); }else{ // $.ajax方法实现 $.ajax({ url:"userInfoController/isExit.do", type:"get", data:"username="+uname, dataType:"text", async:false, success:function(result){ console.log(result) if(result=="用户名可用"){ $("#msg").css("color","green").html(result); }else if(result=="用户名已存在"){ $("#msg").css('color','red').html(result); $("#username")[0].focus(); } $("#msg").html(result); } }); } }); }); </script> 账号:<input type="text" name="username" id="username" > <span id="msg"></span><br> 后端代码 @RequestMapping("userInfoController/isExit") |