jquery last focused element

﹥>﹥吖頭↗ 提交于 2019-12-04 03:22:39

问题


I have a basic login form, which uses ajax to login users. If the details are incorrect text is displayed showing login has failed. When this is shown the last textbox the user entered text in loses focus.

Is it possible to set focus to the last textbox which the user was on before pressing submit/pressing enter?

$('#login').click(function (e) 
 { 
  e.preventDefault();
  $.ajax({
  type: "POST",
  dataType: "text",
  url: "login.php",
  data: "username=" + $("#username").val() + "&password=" + $("#password").val(),
  cache: false,
  success: function(reply)
  {
   if (reply.indexOf("Success") >= 0)
   {
    location.reload();
   }
   else
   {
    $("#loginResult").html("Login Failed");
    //set focus here       
   }
  }
 }); 

Any suggestions, Thanks.


回答1:


Subscribe all of your inputs to the focusout event

$('.input').focusout(function () {
   $('#myform').data('lastSelected', $(this));
});


来源:https://stackoverflow.com/questions/4707450/jquery-last-focused-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!