[英]How to get “make” value from one HTML form to another?
I'm having an issue sharing a value between my HTML forms. 我在HTML表单之间共享值时遇到问题。 I'm a beginner so this is probably a very easy fix. 我是一个初学者,所以这可能是一个非常简单的修复。
Newvehicle.html: Newvehicle.html:
<div class="input-group">
<input class="form-control" id="inputMake" type="text" placeholder="Make..." style="width: 150px;"/>
</div>
Item1.html: Item1.html:
<label>Make: </label><li onclick="getMake()"></li></br>
Newvehicle.js: Newvehicle.js:
function getMake(){
var make = document.getElementById("inputMake").value;
}
I would like the value inputted into the text field on Newvehicle.html to display as a list item on Item1.html. 我希望输入到Newvehicle.html的文本字段中的值在Item1.html上显示为列表项。 Can someone please advise? 有人可以请教吗?
What you could do is, to save the value in localStorage and retrive it in the secound file. 您可以做的是将值保存在localStorage中,然后在secound文件中检索它。
A possible HTML solution would be: 可能的HTML解决方案是:
Newvehicle.html: Newvehicle.html:
<div class="input-group">
<input class="form-control" id="inputMake" type="text" placeholder="Make..." style="width: 150px;" onkeyup="localStorage.value1 = this.value" />
</div>
Item1.html: Item1.html:
<label>Make: </label><li id='entry_1'></li></br>
<script type="text/javascript">
document.getElementById('entry_1').innerHTML = localStorage.value1;
</script>
Newvehicle.js: Newvehicle.js:
not required for that, but nice to have the whole logic in a seperate JS file 不需要这样做,但是很高兴将整个逻辑放在单独的JS文件中
Explanation: 说明:
the onkeyup event fires up each time the usert releases a key on the keyboard, so with each firing we create/replace the value1 in localStorage. 每次用户释放键盘上的键时都会触发onkeyup事件,因此每次触发时,我们都会在localStorage中创建/替换value1。
right after the list element will a javasript code be executed that reads the value from localStorage in your case value1 and replaces the innerHTML. 在list元素之后,将立即执行javasript代码,该代码从您的案例value1中的localStorage读取值并替换innerHTML。
keep in mid that this only works if you work on the same domain.Localstorage keeps the data until you clear the localstorage whit localStorage.clear() 请记住,这仅在您在同一域上工作时才有效。Localstorage会保留数据,直到您清除localStorage.clear()的localstorage。
alternatively you can use sessionStorage instead of localStorage tho keep the data only for one browsing session. 或者,您可以使用sessionStorage代替localStorage或仅保留一个浏览会话的数据。
See: Webstorage on W3C Schools 请参阅: W3C学校上的Webstorage
Keep on going and soon you will master the Javasript language. 继续前进,很快您将掌握Javasript语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.