简体   繁体   English

如何将变量移至另一个html文件并根据该变量更改html文件上的文本

[英]How can I move a variable over to another html file and change the text on the html file based on the variable

First, Im trying to transfer a variable to another html file, but I think I have it totally wrong 首先,我试图将变量传输到另一个HTML文件,但我认为我完全错了

This is my code for the first page: 这是我第一页的代码:

<input type="text" placeholder="Enter Storage Size" name="storage size" id="storagesize" required>
<label for="Colour"><b>Colour</b></label>
<input type="text" placeholder="Enter Colour" name="Colour" id="colour" required>

<label for="Features"><b>Features</b></label>
<form>
<label class="container">FingerPrint Scanning Security 50$
<input type="checkbox" value="50" id="fingerprint">
<span class="checkmark"></span>
</label>

<label class="container">Feature Two 50$
<input type="checkbox" value ="50" id="two">
<span class="checkmark"></span>
</label>

<label class="container">Feature Three 50$
<input type="checkbox" value ="50" id="three">
<span class="checkmark"></span>
</label>

<label class="container">Feature Four 50$
<input type="checkbox" value ="50" id="four>
<span class="checkmark"></span>
</label>
</form>

<script>
var feature;
var size = document.getElementById("userInput").value;
var colour = document.getElementById("userInput").value;
localStorage.size = size
localStorage.colour = colour
('fingerprint').click(function() {
feature ="50";
localStorage.feature="50";
});
('two').click(function() {
feature ="50";
localStorage.feature="50";
});
('three').click(function() {
feature ="50";
localStorage.feature="50";
});
('four').click(function() {
feature ="50";
localStorage.feature="50";
 });

</script>`

This is the code for the other html page: 这是另一个html页面的代码:

  <script>
  var size
  var colour
  var feature
  size = localStorage.size;
  colour = localStorage.colour;
  feature = localStorage.feature;
  document.getElementById("colour").innerHTML = colour;
  document.getElementById("sizetotal").innerHTML = size;
  document.getElementById("featureprice").innerHTML = feature;
  </script>

  <h4>Cart <span class="price" style="color:black"><i class="fa fa-shopping-cart"> 
  </i> <b>4</b></span></h4>
  <p id="colour"> <span class="price">0</span></p>
  <p id="sizetotal"> <span id="sizeprice" class="price"></span></p>      
  <p>Features<span id ="featureprice" class="price"></span></p>
  <hr>
  <p>Total <span class="price" style="color:black"><b></b></span></p>

I just started coding like a week ago, so Im totally lost. 我就像一个星期前才开始编码,所以我完全迷失了。 If someone can help that would be great. 如果有人可以帮助,那就太好了。 Or if im hopeless tell me. 或者如果我没有希望告诉我。

Pass the variable as an argument in the URL, eg www.mysite.com/otherpage?color=blue . 将变量作为参数传递给URL,例如www.mysite.com/otherpage?color=blue Then use JavaScript to get the argument into a variable on the new/other page. 然后使用JavaScript将参数转换为新页面或其他页面上的变量。

var getArg = getArg();
var color = getArg['color'];

function getArg(param) {
    var vars = {};
    window.location.href.replace(window.location.hash, '').replace(
        /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
        function(m, key, value) { // callback
            vars[key] = value !== undefined ? value : '';
        }
    );
    if (param) {
        return vars[param] ? vars[param] : null;
    }
    return vars;
}

I suggest using a server side languaje such as PHP, if you want to create an ecommerce it's the way to go. 我建议使用服务器端语言(例如PHP),如果您想创建电子商务,那是必须走的路。 localstorage is fine for simple (and not fundamental) functionality of your website. localstorage适用于网站的简单(而非基本)功能。

Using a server side language and sessions will be easier and better for a long term application. 对于长期应用程序,使用服务器端语言和会话将更加容易和更好。

暂无
暂无

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

相关问题 如何使用 javascript 变量更改 html 文件中的文本? - How can I change text in an html file using a javascript variable? 如何从.html文件更改.js文件中的变量? - How do I change a variable in a .js file from a .html file? 如何将变量从 javascript 文件传输到 node.js 中的另一个 html 文件? - How can I transfer a variable from a javascript file to another html file in node.js? 如何将 javascript 变量从 HTML 导入另一个 js 文件? - How can import a javascript variable to another js file from HTML? 如何将数据从文本文件读取到变量中,以便可以根据值更改颜色? - How do I read the data from text file into a variable so I can change the color based on the value? 如何在 html 文件中声明一个全局变量,该变量可以被另一个 html 文件以及同一个项目使用 - How to declare a global variable in html file which can be used by another html file as well of same project HTML-如何将 .txt 文件的内容插入到变量中以使用 .innerHTML 更改其内容? - HTML- How can I insert the contents of a .txt file into a variable to use .innerHTML to change the contents of <body>? 如何点击HTML变量更改? - How can I onclick an HTML variable change? 如何将变量从 html 脚本中提取到 JavaScript 文件中? - How can I pull a variable from an html script to a JavaScript file? 我如何在 HTML JS 中打开带有变量链接的本地文件 - how can i open local file with a variable link in HTML JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM