Home >Web Front-end >Front-end Q&A >jquery implements login jump page jump page jump
In web development, a very common requirement is for users to jump to different pages after completing the login operation by entering their account number and password. This process requires the use of the very popular jQuery in the Javascript library.
jQuery is a fast, concise JavaScript library designed with the philosophy of "write less, do more". It encapsulates commonly used operations in JavaScript, allowing developers to complete web development more conveniently and quickly.
In this article, we will introduce how to use jQuery to implement page jump after user login.
First, we need to define a form in HTML that contains username and password input boxes and submit buttons, as shown below:
<form id="login-form"> <div> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> </div> <div> <label for="password">密码:</label> <input type="password" id="password" name="password" required> </div> <button type="submit">登录</button> </form>
In the code, we use the required attribute in HTML5 to force users to fill in their username and password.
Next, we need to use jQuery to bind the form submission event. We can use the $(document).ready(function(){}) method to bind events when the document is loaded, as shown below:
$(document).ready(function(){ $("#login-form").submit(function(event){ // 阻止表单默认提交行为 event.preventDefault(); // 获取用户名和密码 var username = $("#username").val(); var password = $("#password").val(); // TODO: 验证用户名和密码 // 跳转到指定页面 window.location.href = "https://www.example.com/dashboard"; }); });
In the code, we use jQuery's $("# login-form") method selects the form element and uses the .submit() method to bind the submit event. In the event handler function, we use the event.preventDefault() method to prevent the form's default submission behavior.
Next, we obtain the username and password from the input box through the $.val() method, and can perform username and password verification as needed. After passing the verification, we can use the window.location.href property to jump to the specified page.
In the code, we use the window.location.href attribute to jump to the specified page. This attribute can get or set the URL of the current page. You can use a statement like window.location.href = "your_url" to jump to the page.
In our code, we will jump to the page "https://www.example.com/dashboard". Of course, in actual development, you need to replace it with your own jump target.
In the above code, after the user clicks the login button, the page will be redirected to the specified URL. However, this approach has some shortcomings. For example, users may face some delays during password verification. This degrades the user experience, so a better way is to use AJAX to implement asynchronous login and jump.
In order to implement asynchronous requests to log in and jump, we can modify the above code as follows:
$(document).ready(function(){ $("#login-form").submit(function(event){ // 阻止表单默认提交行为 event.preventDefault(); // 获取用户名和密码 var username = $("#username").val(); var password = $("#password").val(); // 发起AJAX请求 $.ajax({ type: "POST", url: "/login", data: {username: username, password: password}, success: function(data){ // 登陆成功,跳转到指定页面 window.location.href = "https://www.example.com/dashboard"; }, error: function(){ // 登录失败 alert('用户名或密码不正确!'); } }); }); });
In the above code, we use jQuery's ajax() method to initiate an asynchronous request. By setting parameters such as type, url, and data, we can send a request to the specified URL. When the server returns a successful response, we use the success() method to jump to the page. If the login fails, the error window pops up through error.
Summary
This article introduces how to use jQuery to log in and jump to a specified page, including binding form submission events, obtaining user names and passwords, verifying user identity, and page jumps. . We also introduced the method of using AJAX to implement asynchronous request login jump. These technologies allow us to better handle user logins and page jumps.
The above is the detailed content of jquery implements login jump page jump page jump. For more information, please follow other related articles on the PHP Chinese website!