[英]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.