我有两个页面,第一个叫做store.php,另一个叫做cart.php。我尝试从store.php页面上的cart.php页面上的表中转移总行数,我有购物车图标,并且将产品数放在其上方。我的想法是通过javascript计数表中的所有行,并将其放在购物车图标上。问题不外乎尝试,我无法从store.php内的cart.php页面中获取所有行的值。
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<title>store</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
</head>
<body>
<div class="Cart">
<a href="cart.php">
<i class="fa fa-shopping-cart" aria-hidden="true"></i>
<span class="Cart_Number" id="Cart_Number"></span>
</a>
</div>
<script type="text/javascript" src="Counter_Cart.js"></script>
</body>
</html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<title>cart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
</head>
<body>
<table style="width:100%" id="Product_Map">
<tr>
<th>Product name</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td>Product 1</td>
<td>Product 1</td>
<td>Product 1</td>
</tr>
<tr>
<td>Product 2</td>
<td>Product 2</td>
<td>Product 2</td>
</tr>
<tr>
<td>Nothing</td>
<td>Nothing</td>
<td>Total</td>
</tr>
</table>
<script type="text/javascript" src="Counter_Cart.js"></script>
</body>
</html>
function CountRowsUsingJavascript() {
var totalRowCount = 0;
var table = document.getElementById("Product_Map");
var rows = table.getElementsByTagName("tr");
for (var i = 0; i < rows.length; i++) {
if (i > 0) {
totalRowCount++;
} else {
totalRowCount = 0;
}
}
if (totalRowCount > 0) {
totalRowCount = totalRowCount - 1;
return totalRowCount;
} else {
return totalRowCount;
}
}
var Value_For_The_Cart = CountRowsUsingJavascript();
答案 0 :(得分:0)
要回答您的问题,我可以想到两种方法:
window.name
是一种特殊变量,只要用户停留在同一选项卡上,它就会在页面重新加载和导航期间保持其值。此变量仅包含字符串,因此您可以将数字转换为字符串。
window.name = Value_For_The_Cart.toString();
本地存储用于存储持久存储在用户浏览器中的数据,因此可以跨页面保存。要将某些内容保存到本地存储中,
localStorage.setItem('whateverNameYouWant', Value_For_The_Cart.ToString());
并得到它
localStorage.getItem('whateverNameYouWant');
但是我建议您考虑处理此客户端是否是解决此问题的最佳方法。