[英]How do I display user input using document.getElementById() from one webpage on another using javascript?
I am trying to record user input in listing.html
in order to display it on my main page index.html
.我试图在listing.html
中记录用户输入,以便在我的主页index.html
上显示它。 To do this, I am using a function I wrote, namely submitValue()
with a nested function addListing()
.为此,我使用了我编写的函数,即submitValue()
和嵌套函数addListing()
。
listing.html:列表.html:
<h3>
Timer: <input id="myTimer">
<p>
Question: <input id="myQuestion">
<p>
Bounty: <input id="myBounty">
</h3>
<button onclick="submitValue()"> Submit </button>
<script src="index.js"></script>
submitValue()
is located in index.js
(where I initially planned to write all of my javascript code). submitValue()
位于index.js
(我最初计划在其中编写我所有的 javascript 代码)。
function submitValue() {
var t = document.getElementById("myTimer").value;
var q = document.getElementById("myQuestion").value;
var b = document.getElementById("myBounty").value;
location.href='index.html';
addListing('#f38181', t, q, b);
}
The problem I have encountered is that the function document.getElementById()
refers automatically to the main page (which in my case is index.html
).我遇到的问题是函数document.getElementById()
自动引用主页面(在我的例子中是index.html
)。 The divs "myTimer"
, "myQuestion"
, and "myBounty"
are located on listing.html
however.然而,div "myTimer"
、 "myQuestion"
和"myBounty"
位于listing.html
。 This is inconvenient for my purposes as, once again, I'm trying to display the user input (value) from these divs on index.html
.这对我的目的来说很不方便,因为我再次尝试在index.html
上显示来自这些 div 的用户输入(值)。 I've read several forum posts that vaguely hinted at using AJAX and JQuery, as well as document.cookie
.我读过一些论坛帖子,这些帖子含糊地暗示使用 AJAX 和 JQuery 以及document.cookie
。 I'm open to trying any approach as long as it works.只要可行,我愿意尝试任何方法。 If you've made it through all this preamble, then my question is as follows: How do I solve this annoying problem?如果您已经完成了所有这些序言,那么我的问题如下:如何解决这个烦人的问题?
The data you retrieve fro the listing.html does not persist if you call another page, in your case index.html .如果您调用另一个页面(在您的情况下为index.html ) ,您从 listing.html 检索的数据不会保留。 In order to achieve the result you have to persist the data you get from the fields.To persist the data either you can use a db from server or you can use local storage from browser.为了实现结果,您必须保留从字段中获得的数据。要保留数据,您可以使用来自服务器的数据库,也可以使用来自浏览器的本地存储。
In chrome you can try this.在 chrome 中你可以试试这个。
function submitValue() { var t = document.getElementById("myTimer").value; var q = document.getElementById("myQuestion").value; var b = document.getElementById("myBounty").value; localStorage.setItem("#f38181",JSON.stringify({ t, q, b})) location.href='index.html'; }
And in the index.html retrieve the data like this并在 index.html 中检索这样的数据
var listingdata = localStorage.getItem("#f38181"); var data = JSON.parse(listingdata); console.log(listingdata)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.