简体   繁体   English

如何从一个 HTML 文件中读取数据并使用 JavaScript 将其显示在另一个文件中

[英]How do I read data from one HTML file and display it in another with JavaScript

I have one HTML file that contains a table with items and their price.我有一个 HTML 文件,其中包含一个包含项目及其价格的表。 I successfully got the javascript working to traverse the table and calculate the sum of the prices, but only when it's inside the same html file.我成功地让 javascript 能够遍历表格并计算价格总和,但前提是它位于同一个 html 文件中。 I would like to display this calculation in a separate html file and I'm having trouble doing so.我想在单独的 html 文件中显示此计算,但我在这样做时遇到了麻烦。

//testTable.html

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8"/>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
</head>

<body>

<h2>Html Table</h2>

<table id="EstoreTable" style="width:100%">
  <tr>
    <th>Item Name</th>
    <th>Quantity</th> 
    <th>Price</th>
  </tr>
  <tr>
    <td>iPhone X</td>
    <td>10</td>
    <td>299.99</td>
  </tr>
  <tr>
    <td>Apple Watch 3</td>
    <td>12</td>
    <td>250.00</td>
  </tr>
  <tr>
    <td>MacBook Pro 13" </td>
    <td>7</td>
    <td>1199.99</td>
  </tr>
</table>
    <a href="assign1_agm75_checkout.html" target="_blank" id="checkout">Checkout</a>
    <script src="ShoppingAmount.js"></script>
    </body>  

</html>

//ShoppingAmount.js

function getCheckoutAmounts()
{ 
    var table = document.getElementById('EstoreTable'); //data from TestTable.html
    var sum = 0,
        taxTotal = 0,
        tax = .0825,
        shipping = 5.99,
        due = 0;

    for (var i = 1; i < table.rows.length; i++)
        {
           sum = sum + parseFloat(table.rows[i].cells[2].innerHTML); //stores total price into sum
        }

    taxTotal = tax * sum; //stores calculated tax

    due = sum + taxTotal + shipping;

    // SET VIA query string
    this.href += "?sum=" + sum;
    this.href += "?taxTotal=" + taxTotal;
    this.href += "?shipping=" + shipping;
    this.href += "?due=" + due;

    // SET SUM VIA localStorage
    window.localStorage.setItem("sum", sum);
    window.localStorage.setItem("taxTotal", taxTotal);
    window.localStorage.setItem("shipping", shipping);
    window.localStorage.getItem("due", due);

}

document.querySelector("#checkout").addEventListener("click", getCheckoutAmounts);


//Checkout.html

<html>
<h1>Checkout</h1>

Total Shopping Amount: $<span id="Shopamount"></span><br>

Total Tax: $<span id="TaxTotal"></span><br>

Total Shipping Charges: $<span id= "charges"></span><br>

Total Amount Due: $<span id="total"></span><br>
<script>

    // GET data VIA localStorage
    document.querySelector("#Shopamount").textContent = window.localStorage.getItem("sum");
    document.querySelector("#TaxTotal").textContent = window.localStorage.getItem("taxTotal");
    document.querySelector("#charges").textContent = window.localStorage.getItem("shipping");
    document.querySelector("#total").textContent = window.localStorage.getItem("due");


    // GET SUM VIA query string
    let queryString = document.location.search;
    let sum = queryString.split("=")[1];
    let taxTotal = queryString.split("=")[1];
    let shipping = queryString.split("=")[1];
    let due = queryString.split("=")[1];

    document.querySelector("#Shopamount").textContent = sum;
    document.querySelector("TaxTotal").textContent = taxTotal;
    document.querySelector("charges").textContent = shipping;
    document.querySelector("total").textContent = due;
</script>  

The goal is to get it to print in Checkout.html but I've been at this for hours with no results.目标是让它在 Checkout.html 中打印,但我已经做了几个小时没有结果。 please help.请帮忙。 [UPDATE]: After some help from @Kostax, I managed to get some of the calculations displayed, but it's mixed with some bugs. [更新]:在@Kostax 的帮助下,我设法显示了一些计算,但它与一些错误混合在一起。

Here's a basic setup to get you started.这是帮助您入门的基本设置。 The JS in testTable, calculates the total, once the Checkout button is clicked and stores the sum in localStorage. testTable 中的 JS 计算总数,一旦单击 Checkout 按钮并将总和存储在 localStorage 中。 The link then gets triggered and redirects you to the checkout page, where the sum value is retrieved from localStorage and displayed in the page.然后链接会被触发并将您重定向到结帐页面,其中从 localStorage 检索总和值并显示在页面中。

testTable.html :测试表.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        table,
        th,
        td {
            border: 1px solid black;
        }
    </style>

</head>
<body>

    <h2>Html Table</h2>

    <table id="EstoreTable" style="width:100%">
        <tr>
            <th>Item Name</th>
            <th>Quantity</th>
            <th>Price</th>
        </tr>
        <tr>
            <td>iPhone X</td>
            <td>10</td>
            <td>299.99</td>
        </tr>
        <tr>
            <td>Apple Watch 3</td>
            <td>12</td>
            <td>250.00</td>
        </tr>
        <tr>
            <td>MacBook Pro 13" </td>
            <td>7</td>
            <td>1199.99</td>
        </tr>
    </table>
    <a href="Checkout.html" target="_blank" id="checkout">Checkout</a>
    <script src="ShoppingAmount.js"></script> 
</body>

</html>

ShoppingAmount.js :购物金额.js

function getShoppingAmount(e) {

    var table = document.getElementById('EstoreTable'); //data from TestTable.html
    var sum = 0;

    for (var i = 1; i < table.rows.length; i++) {
        sum = sum + parseFloat(table.rows[i].cells[2].innerHTML); //stores total price into sum
    }

    // SET SUM VIA query string
    this.href += "?sum=" + sum; 

    // SET SUM VIA localStorage
    window.localStorage.setItem( "sum", sum );

}

document.querySelector("#checkout").addEventListener("click", getShoppingAmount );

Checkout.html :结帐.html

<html>
<h1>Checkout</h1>

Total Shopping Amount: <span id="Shopamount" ></span>
<script>
    // GET SUM VIA localStorage
    document.querySelector("#Shopamount").textContent = window.localStorage.getItem("sum");

    // GET SUM VIA query string
    let queryString = document.location.search;
    let sum = queryString.split("=")[1];
    document.querySelector("#Shopamount").textContent = sum;
</script>

[ UPDATED ] to include another way of passing data between two pages via the query string mechanism as pointed out by @Jonathan Gray [ 更新 ]包括通过@Jonathan Gray 指出的查询字符串机制在两个页面之间传递数据的另一种方式

暂无
暂无

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

相关问题 如何从另一个HTML文件中的一个HTML Apps脚本文件获取JSON数据? - How do I get JSON data from one HTML Apps Script file in another HTML file? 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 如何显示一页到另一页的数据? - How do i display a data from one page to another? 如何将变量从一个 javascript 文件传递到另一个文件? - How do I pass a variable from one javascript file to another? 如何使用 javascript 在另一个网页上使用 document.getElementById() 显示用户输入? - How do I display user input using document.getElementById() from one webpage on another using javascript? 如何从一个 html 表单中获取数据并将其显示在另一个表单上? - How can I get a data from one html form and display it on another? 如何从 csv 文件读取和显示数据到 html 网页? - How to read and display data from a csv file to html webpage? 如何将 JSON 文件数据显示到 HTML 表中(仅使用 JavaScript,而不是 jQuery) - How do I display JSON file data into HTML table (using JavaScript only, not jQuery) 在ASP中,如何从用VB编写的另一个.asp文件中的一个.asp文件中调用JavaScript函数? - In ASP how do I call a JavaScript function in one .asp file from another .asp file written in VB? 如何从一个主文件调用另一个JavaScript文件? - How do I call up another JavaScript file from one main file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM