简体   繁体   English

页面加载后如何关注输入字段?

[英]how to focus on input field after page load?

In my page I have a html form which saves data using jquery/ajax. 在我的页面中,我有一个html表单,该表单使用jquery / ajax保存数据。 I am calling jquery/ajax when the users mouse leaves the input field. 当用户鼠标离开输入字段时,我正在调用jquery / ajax。 So for example I have the following fields: 因此,例如,我有以下字段:

1. name : <input type="text" name="name" id="name">
2. email : <input type="text" name="email" id="email">
3. mobile : <input type="text" name="mobile" id="mobile">

So when the users mouse leaves any of the fields, jquery/ajax is used to store the data in the database. 因此,当用户鼠标离开任何字段时,将使用jquery / ajax将数据存储在数据库中。

Now my questions is: When I am typing in the 'name'-field and then I immediately switch to the second field, the 'email'-field using keyboard tab. 现在我的问题是:当我在“名称”字段中键入内容时,我立即切换到第二个字段,即使用键盘选项卡的“电子邮件”字段。 After that it's loading the page because I am calling it when the mouse leaves from a field. 之后,它正在加载页面,因为当鼠标从字段离开时我正在调用它。 Now I want to focus on the second field when the page is loaded. 现在,我想在页面加载时专注于第二个字段。 Not only for the second field, it should work for any field. 不仅适用于第二个领域,它还应适用于任何领域。 How to achieve this? 如何实现呢?

在ajax调用的成功部分中,您可以指向下一个输入,例如:
$("#email).focus();

Why are loading page again if you are doing ajax call? 如果执行ajax调用,为什么还要再次加载页面? Just load the section which you want to really update after ajax save. 保存ajax之后,只需加载您要真正更新的部分即可。 If you want to show another section as per open/action value selection you can do that without page loading. 如果要根据打开/操作值选择显示另一部分,则可以在不加载页面的情况下进行操作。

If that section is already available in DOM with hide mode then after success ajax call just make that show . 如果该部分已经可以在具有hide模式的DOM中使用,那么在成功执行ajax调用后,只需进行show But if the section is not really exist in the DOM then after ajax call just generate the HTML content for that section on the fly and then put it in your DOM where you need it. 但是,如果该部分不是真正存在于DOM中,那么在ajax调用之后,只需立即为该部分生成HTML内容,然后将其放入您需要的DOM中即可。

That's all. 就这样。 Ajax call are basically used to not reload the page. Ajax调用基本上用于不重新加载页面。 If you are loading the page then ajax call is not require at all you can just do that in normal post way. 如果您正在加载页面,则根本不需要ajax调用,您可以通过常规post方式进行。

Hope this will helps you! 希望这对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM