jQuery最后一个重点元素 [英] jquery last focused element

查看:111
本文介绍了jQuery最后一个重点元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的登录表单,该表单使用ajax登录用户.如果详细信息不正确,则会显示文本,表明登录失败.显示最后一个文本框时,用户输入的文本将失去焦点.

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.

是否可以在按提交/按Enter之前将焦点设置到用户所在的最后一个文本框?

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       
   }
  }
 }); 

任何建议,谢谢.

推荐答案

将所有输入订阅到焦点事件

Subscribe all of your inputs to the focusout event

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

这篇关于jQuery最后一个重点元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆