简体   繁体   English

如何使用 AJAX 将参数从 1 个 HTML 页面传递到另一个 HTML 页面?

[英]How can I pass parameter from 1 HTML page to another HTML page with AJAX?

This is my first page.这是我的第一页。

<!DOCTYPE html>
<html>
<head>
    <title>Student List</title>
</head>
<body>
        <h1>Customer Form</h1>
        <form>
        <div>
            <label>Customer Name:</label>
            <input type="text" autofocus id="name">
        </div>
        <br>
        <div>
            <label>Address:</label>
            <input type="text" id="address">
        </div>
    <button  type="button" onclick="myFunction()">Submit</button>
        </form>
    
    <!-- JavaScript -->
    <script type="text/javascript" src="./javascript.js"></script>
</body>
</html>

This is my second page.这是我的第二页。

<!DOCTYPE html>
<html>
<head>
    <title>Student List</title>
</head>
<body>
        
    <p id="1"></p>
        
    <!-- JavaScript -->
</body>
</html>

MY JAVASCRIPT我的 Java 脚本

function myFunction() {
    const xhttp = new XMLHttpRequest();
    xhttp.onload = function(){
        name = document.getElementById('name').value;
        address = document.getElementById('address').value;
        document.getElementById("1").innerHTML="<h1>alola="+name+"</h1>";
        document.getElementById("2").innerHTML=address;
    }
    xhttp.open("GET", name);
    xhttp.send();
}

I would like to display my name when typing my name from first page in the customer name label, and when i click submit.我想在客户姓名标签的第一页中输入我的姓名以及单击提交时显示我的姓名。 my second page display my name in the "id=1".我的第二页在“id=1”中显示我的名字。

Ajax and ajax like techniques (fetch etc.) are usually used to either post data to a form or to get data from a url. Ajax 和类似 ajax 的技术(fetch 等)通常用于将数据发布到表单或从 url 获取数据。

To send data from one HTML page to another you would need some kind of database and server side code to capture and store it and later retrive it.要将数据从一个 HTML 页面发送到另一个页面,您需要某种数据库和服务器端代码来捕获和存储它,然后再检索它。

If you just want inputed client side data to persist across pages then look at sessionStorage or localStorage , for example to keep the name we type until we exit the browser or clear session data:如果您只是希望输入的客户端数据跨页面持久化,请查看sessionStoragelocalStorage ,例如,在退出浏览器或清除会话数据之前保留我们键入的名称:

First page:第一页:

<form>
        <div>
            <label>Customer Name:</label>
            <input type="text" autofocus id="name">
        </div>
        <br>
        <div>
            <label>Address:</label>
            <input type="text" id="address">
        </div>
    <button  type="button" onclick="">Submit</button>
        </form>
<script>
  const name=document.getElementById('name')
  name.addEventListener("keyup", (e) => {
      
  sessionStorage.setItem('uname', name.value);
});
</script>

Second page:第二页:

<body>
        
  <p id="usersName"></p>
  <script>
    let userNameElm = document.getElementById('usersName');
    userNameElm.innerText = sessionStorage.getItem('uname');
  </script>   
</body>

Just remember to be careful what you store, and that it's only stored on the frontend/client side.请记住要小心您存储的内容,并且它仅存储在前端/客户端。

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

相关问题 如何使用javascript将值从一个html页面传递到另一个html页面 - How can I pass values from one html page to another html page using javascript 我可以从HTML的另一个页面回呼AJAX吗? - Can I make a AJAX call back from another page in HTML? 如何从另一个页面更改页面的html代码? - How can I change html code of a page from another page? 如何将数据从html页面传递到另一个页面? - How to pass data from a html page to another? 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何使用JavaScript将值从一个HTML页面传递到另一个HTML页面? - How can I pass a value from one HTML page to another using JavaScript? 如何使用 JavaScript 将数据从一个 HTML 页面传递到另一个页面 - How can i pass data from one HTML page to another using JavaScript 如何使用JavaScript将列表组之类的数据从一个HTML页面传递到另一HTML页面? - How can I pass data like list group from one html page to another using JavaScript? 如何从谷歌 appscript 中的另一个 html 页面调用另一个 HTML 页面 - How can I call another HTML page from another html page in google appscript 如何将数组从一个HTML传递到另一个HTML页面? - how to pass an array from one html to another html page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM