简体   繁体   English

如何在此 HTML 页面中存储数据并使用 JavaScript 在另一个页面中检索它

[英]How do I store data in this HTML page and retrieve it in another using JavaScript

I want the first name to be stored and then retrieved and displayed on another html page.我希望存储名字,然后检索并显示在另一个 html 页面上。 Can someone baby walk me through it please as I have been having a very hard time with this.有人能帮我看看吗,因为我一直在努力解决这个问题。 Do I need 1 JavaScript external file?我需要 1 个 JavaScript 外部文件吗? If not, which JavaScript files do I link to which HTML files.如果没有,我链接到哪个 JavaScript 文件到哪个 HTML 文件。

 <section class="enquirything"> <div class="containerr"> <form action="payment.html" id="regform" method="post"> <label for="fname">First Name</label> <input type="text" id="fname" name="firstname" required="required" placeholder="Your name.." maxlength="25" onkeypress="return (event.charCode > 64 && event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || (event.charCode==32)">

A better version:更好的版本:

Index.html:索引.html:

<section class="enquirything">
  <div class="containerr">
    <form action="something.html" id="regform" method="get">
      <label for="fname">First Name</label>
      <input type="text" id="fname" name="firstname" required="required" placeholder="Your name.." maxlength="25" onkeypress="return (event.charCode > 64 && event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || (event.charCode==32)">
      
      <input type="submit" value="Submit the form">
      
    </form>
  </div>
</section>

Something.html:东西。html:

<span id="firstName">Hello!</span>
<script src="file.js"></script>

File.js:文件.js:

function parseGetParam(p) {
    if (!p) return {};
    return JSON.parse(`${p.split('?').join('{"').split('=').join('":"').split('&').join('","')}"}`);
}

let get = parseGetParam(location.search);

function receiveFirstName() {
    let firstName;
    if (typeof get["firstname"] === "undefined")
        firstName = "some random dude";
    else
        firstName = get["firstname"];
    return firstName;
}

const firstName = receiveFirstName();
let fnElement = document.getElementById("firstName");
fnElement.innerHTML = `Hello, ${firstName}! This is JS!`;

You can improve it, if you tinker with it!你可以改进它,如果你修补它!

暂无
暂无

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

相关问题 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何使用javascript中的刷新功能发送数据并使用相同的刷新功能检索其他数据 - How do I send data using a refresh function in javascript and retrieve another data using the same refresh function 使用Localstorage存储和检索从一个html页面到另一个html页面的javascript变量(DOM) - Using Localstorage to store and retrieve javascript variables from one html page to another (DOM) 如何检索输入到下一个HTML页面的调查单选按钮? 使用本地存储和纯JavaScript - How do I retrieve survey radio buttons input to the next HTML page? Using local storage and pure javascript 如何过滤 JSON 文件以使用 Javascript 检索 HTML 数据 - How do I Filter a JSON file to retrieve data for HTML with Javascript 如何使用Ajax从一个HTML页面中使用数据来检索要在另一HTML页面上使用的数据 - How to use data from one HTML page to retrieve data to be used on another HTML page using ajax 如何通过HTML表单输入文本并使用Javascript从JSON文件中检索数据作为响应? - How do I input text via an HTML form and retrieve data from a JSON file as a response using Javascript? 如何使用JavaScript从html5 canvas检索原始位图数据? - How do I retrieve raw bitmap data from html5 canvas using JavaScript? 如何使用PhoneGap,HTML / JavaScript和jQuery Mobile存储数据? - How do I store data using PhoneGap, HTML/JavaScript, and jQuery Mobile? 如何使用 javascript 从表单存储数据并将其显示在新页面上? - How do I store data from form and display it on a new page using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM