简体   繁体   English

如何使网站记住上次显示的表格?

[英]How can I make the website remember last shown form?

I have two forms inside one div: 我在一格内有两种形式:

  • Login form 登录表单
  • Register form 登记表格

Register form is hidden while login form is shown as default when you load the page. 加载页面时,注册表单是隐藏的,而登录表单则显示为默认表单。 Im using js script to animate between those two forms by clicking anchor tag. 我通过单击锚标记使用js脚本在这两种形式之间建立动画。

This causes problems for example while trying to register and you don't go through validation process correctly. 例如,这会导致尝试注册时出现问题,并且您无法正确通过验证过程。

Hypothetical situation: 假设情况:

You've filled all the inputs, click submit button and it turns out that username you've provided is too short. 您已填写所有输入,单击提交按钮,结果发现您提供的用户名太短。 The page will reload and it will show the default form again which is login form. 该页面将重新加载,并再次显示默认表单即登录表单。 You then again need to click "Register" a tag in order to switch between forms and then you see the error with validation. 然后,您再次需要单击“注册”标签以在表单之间切换,然后看到验证错误。

I've been trying to read up about ways of doing that for days but seems like I can't specify my problem well enough in a short phrase. 几天来,我一直在尝试阅读有关执行此操作的方法的信息,但是似乎我无法用一个简短的短语充分说明我的问题。 Can you guys explain what kind of method should I use and explain how to use it? 你们能解释我应该使用哪种方法并解释如何使用它吗?

HTML: HTML:

<div class="login-page">
        <div class="form">
            <form class="register-form" method="POST">
                <input type="text" name="nick" placeholder="username"/>
                <input type="password" name="password1" placeholder="password"/>
                <input type="password" name="repassword" placeholder="repeat password"/>
                <input type="text" name="email" placeholder="e-mail adress"/>
                <button>Create</button>
                <p class="message">Already have an account? <a href="#">Log in!</a></p>
            </form>
            <form class="login-form" method="POST" action="login.php">
                <input type="text" name="login" placeholder="username"/>
                    <input type="password" name="password" placeholder="password"/>
                    <button>log in</button>
                    <p class="message">Don't have an account yet? <a id="createacc" href="#">Sign up!</a></p>
            </form>

JS: JS:

$('.message a').click(function(){
$('form').animate({height: "toggle", opacity: "toggle"}, "slow"});

I want the website to remember which form you were filling before the page reloaded instead of always showing the default one (which is login form). 我希望网站记住在重新加载页面之前您要填写的表格,而不是始终显示默认表格(登录表格)。

EDIT : In case anyone wondering. 编辑以防万一。 Yes, I did create similiar post about this yesterday but I realised that I didn't explain this well enough. 是的,昨天我确实创建了与此类似的帖子,但我意识到自己对此的解释不够充分。 Hope this goes through well. 希望这一切顺利。

You could make use of sessionStorage , a similar mechanism to cookies that persist only for the current session. 您可以使用sessionStorage ,一种类似于cookie的机制,仅在当前会话中持续存在。 When switching the visibility of your forms, set accordingly which one is also shown: 切换表单的可见性时,请相应地设置还显示哪一个:

window.sessionStorage.setItem('lastOpenedForm', 'register');

And once the page loads again, fetch the value from there and display that form: 一旦页面再次加载,就从那里获取值并显示该形式:

window.sessionStorage.getItem('lastOpenedForm');

Adapted code in snippet below: 摘录如下的代码:

 var lastOpenedForm = window.sessionStorage.getItem("lastOpenedForm"); // By default show login form; check if we should show register and toggle visibilities if (lastOpenedForm === "register-form") { console.log('last opened form was register form'); $(".form form").toggleClass("active-form"); $(".login-page").toggleClass("bigger"); } $(".message a").click(function() { $(".form form").toggleClass("active-form"); var activeForm = $('.active-form').eq(0).attr('id'); window.sessionStorage.setItem('lastOpenedForm', activeForm); console.log('last active form is ' + activeForm); $(".login-page").toggleClass("bigger"); }); // $("#createacc").click(function() { // window.sessionStorage.setItem("lastOpenedForm", "register-form"); // }); 
 .login-page { width: 600px; height: 290px; background: #333333; border-radius: 50px; margin-left: auto; margin-right: auto; margin-top: 10px; box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.5), 0 10px 10px 0 rgba(0, 0, 0, 0.24); transition: all 1s; } .login-page.bigger { height: 420px; } .form { position: relative; max-width: 360px; margin: auto; padding: 45px; text-align: center; } .form input { font-family: "Roboto", sans-serif; outline: 0; background: #f2f2f2; width: 100%; border: 0; margin: 0 0 15px; padding: 15px; box-sizing: border-box; font-size: 14px; border-radius: 5px; } .form button { font-family: "Roboto", sans-serif; text-transform: uppercase; outline: 0; background: #666666; width: 100%; border: 0; padding: 15px; color: #FFFFFF; font-size: 14px; cursor: pointer; border-radius: 5px; } .form button:hover, .form button:active, .form button:focus { background: #808080; } .form .message { margin: 15px 0 0; color: white; font-size: 16px; } .form .message a { color: #1a8cff; text-decoration: none; } .form form { /* display: none; */ visibility: hidden; opacity: 0; height: 0; transition: 0.5s easy-in-out all; } .form .active-form { visibility: visible; opacity: 1; height: auto; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="login-page"> <div class="form"> <form id="register-form" method="POST"> <input type="text" name="nick" placeholder="username" /> <input type="password" name="password1" placeholder="password" /> <input type="password" name="repassword" placeholder="repeat password" /> <input type="text" name="email" placeholder="e-mail adress" /> <button>Create</button> <p class="message">Already have an account? <a href="#">Log in!</a></p> </form> <form id="login-form" method="POST" class="active-form" action="login.php"> <input type="text" name="login" placeholder="username" /> <input type="password" name="password" placeholder="password" /> <button>log in</button> <p class="message">Don't have an account yet? <a id="createacc" href="#">Sign up!</a></p> </form> </div> 

Also available in this codepen . 也可在此Codepen中使用

If you're relying on JavaScript then you have a few possible routes. 如果您依赖JavaScript,那么有几种可能的方法。

  1. Use localStorage to set and get the state. 使用localStorage设置并获取状态。 https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
  2. Use a cookie to set and get the state. 使用Cookie设置并获取状态。 https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
  3. Use a query string parameter as part of your url and use the Location api to get it. 将查询字符串参数用作您的网址的一部分,并使用Location api进行获取。 https://developer.mozilla.org/en-US/docs/Web/API/Location https://developer.mozilla.org/en-US/docs/Web/API/Location

Javascript has no way of remembering state so you must make use of a strategy such as the above. Javascript无法记住状态,因此您必须使用上述策略。 Personally I would opt for localStorage as it has a nice simple API. 我个人会选择localStorage,因为它有一个很好的简单API。

You can use localStorage to store the state in your browser. 您可以使用localStorage将状态存储在浏览器中。

<div class="login-page">
        <div class="form">
            <form class="register-form" method="POST" id="register">
                <input type="text" name="nick" placeholder="username"/>
                <input type="password" name="password1" placeholder="password"/>
                <input type="password" name="repassword" placeholder="repeat password"/>
                <input type="text" name="email" placeholder="e-mail adress"/>
                <button>Create</button>
                <p class="message">Already have an account? <a href="#">Log in!</a></p>
            </form>
            <form class="login-form" method="POST" action="login.php" id="login">
                <input type="text" name="login" placeholder="username"/>
                    <input type="password" name="password" placeholder="password"/>
                    <button>log in</button>
                    <p class="message">Don't have an account yet? <a id="createacc" href="#">Sign up!</a></p>
            </form>


var fo;
$(document).ready(function(){

console.log(localStorage.getItem('form'))

$('form').click(function(){fo=$(this).attr('id');
localStorage.setItem('form',fo);
})
})

Working fiddle 工作提琴

You might consider using a URL fragment at the end of your form action attribute--eg <form action="example.com/#register"> . 您可以考虑在表单动作属性的末尾使用URL片段,例如<form action="example.com/#register">

URL fragments are typically used to jump to different sections on a page. URL片段通常用于跳转到页面的不同部分。 So in this case, they're probably more semantically meaningful than other options like session storage, cookies, or query strings. 因此,在这种情况下,它们可能比会话存储,cookie或查询字符串之类的其他选项在语义上更有意义。

You just need to listen to the document's load event to determine which form is being targeted. 您只需要侦听文档的load事件即可确定要定位的表单。 Something like this: 像这样:

$(function () {
    if (window.location.hash == "register") {
        // show registration form here
    }
});

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

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