![](/img/trans.png)
[英]How can I change text in an html file using a javascript variable?
[英]How can I move a variable over to another html file and change the text on the html file based on the variable
首先,我试图将变量传输到另一个HTML文件,但我认为我完全错了
这是我第一页的代码:
<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>`
这是另一个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>
我就像一个星期前才开始编码,所以我完全迷失了。 如果有人可以帮助,那就太好了。 或者如果我没有希望告诉我。
将变量作为参数传递给URL,例如www.mysite.com/otherpage?color=blue
。 然后使用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;
}
我建议使用服务器端语言(例如PHP),如果您想创建电子商务,那是必须走的路。 localstorage适用于网站的简单(而非基本)功能。
对于长期应用程序,使用服务器端语言和会话将更加容易和更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.