简体   繁体   English

如何从一个 html 表单中获取数据并将其显示在另一个表单上?

[英]How can I get a data from one html form and display it on another?

I'm using only JS and I want to get a username from one html and display it on the another html page.我只使用 JS,我想从一个 html 获取用户名并将其显示在另一个 html 页面上。 I'm a beginner on the programming and I have two questions:我是编程初学者,我有两个问题:

  • Can I use only one JS file to do this?我可以只使用一个 JS 文件来执行此操作吗? If yes, how?如果是,如何?

  • Why it isn't working?为什么它不起作用?

First page第一页

    <main>
        <form action="">
           <input type="text" name="login" id="login" class="login" placeholder="Login">
           <a href="password.html" id='logar'>LOGAR!</a>
        </form>
        <span id='spanlogin'></span>
    </main>
    <script src="main.js"></script>
var login = document.getElementById('login').value;

localStorage.setItem("thelogin", login);

Second Page第二页

    <main>
        <div>
            <span id='showlogin'></span>
        </div>
        <div class="senha">
        </div>
    </main>
    <script src="second.js"></script>
var ologin = localStorage.getItem('thelogin');

function showthelogin(){
    document.getElementById('showlogin').innerHTML = ologin
}

window.onload = showthelogin()

Thanks for the help!谢谢您的帮助!

"For documents loaded from file: URLs (that is, files opened in the browser directly from the user's local filesystem, rather than being served from a web server) the requirements for localStorage behavior are undefined and may vary among different browsers. “对于从文件加载的文档:URL(即直接从用户的本地文件系统在浏览器中打开的文件,而不是从 web 服务器提供的文件),对本地存储行为的要求是未定义的,并且可能因不同的浏览器而异。

In all current browsers, localStorage seems to return a different object for each file: URL.在所有当前浏览器中,localStorage 似乎为每个文件返回不同的 object:URL。 In other words, each file: URL seems to have its own unique local-storage area.换句话说,每个文件:URL 似乎都有自己独特的本地存储区域。 But there are no guarantees about that behavior, so you shouldn't rely on it because, as mentioned above, the requirements for file: URLs remains undefined."但是不能保证这种行为,所以你不应该依赖它,因为如上所述,对 file: URLs 的要求仍未定义。”

Its from developer.mozilla.org/en-US/docs/Web/API/Window/localStorage它来自 developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

For what it's worth, the login button is just a link, it's not running the js on the first page.值得一提的是,登录按钮只是一个链接,它并没有在第一页运行 js。 The js executes immediately on page load when the form is still empty.当表单仍然为空时,js 在页面加载时立即执行。 If you wrap it in a function and call the function on click, it will do what you want.如果您将其包装在 function 中并在单击时调用 function,它将执行您想要的操作。

What you want, might not be what you need, but getting things to do what we want them to do can help us better understand what we need.你想要的,可能不是你需要的,但是让事情做我们想让他们做的事情可以帮助我们更好地理解我们需要什么。

main.js main.js

function savePassword(){
    var login = document.getElementById('login').value;
    localStorage.setItem("thelogin", login);    
}

first page:第一页:

<a href="password.html" id='logar' onclick="savePassword()">LOGAR!</a>

In answer to your first question, you can do it with just one page.在回答您的第一个问题时,您只需一页即可完成。 For example, read about Single-page applications .例如,阅读单页应用程序

Note that the way you're doing it is not the same thing as 'submitting' the form .请注意,您执行此操作的方式与“提交”表单不同。

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

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