如何使用javascript从一个网页到另一个网页使用document.getElementById()显示用户输入?

如何使用javascript从一个网页到另一个网页使用document.getElementById()显示用户输入?,javascript,jquery,html,cookies,getelementbyid,Javascript,Jquery,Html,Cookies,Getelementbyid,我试图在listing.html中记录用户输入,以便在我的主页index.html上显示它。为此,我使用了我编写的一个函数,即submitValue()和一个嵌套函数addListing() listing.html: <h3> Timer: <input id="myTimer"> <p> Question: <input id="myQuestion"> <p> Bounty: <input id="myBounty">

我试图在
listing.html
中记录用户输入,以便在我的主页
index.html
上显示它。为此,我使用了我编写的一个函数,即
submitValue()
和一个嵌套函数
addListing()

listing.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>
我遇到的问题是函数
document.getElementById()
自动引用主页面(在我的例子中是
index.html
)。但是,div
“myTimer”
“myQuestion”
“myBounty”
位于
listing.html
上。这对我来说很不方便,因为我再次尝试在
index.html
上显示来自这些div的用户输入(值)。 我读过几篇论坛帖子,其中隐约暗示使用AJAX和JQuery,以及
document.cookie
。只要有效,我愿意尝试任何方法。
如果您已经看完了所有这些前言,那么我的问题如下:如何解决这个恼人的问题?

如果调用另一个页面,您从listing.html中检索到的数据不会持久,在您的例子中是index.html。 为了获得结果,您必须持久化从字段中获取的数据。要持久化数据,您可以使用服务器中的数据库,也可以使用浏览器中的本地存储

在chrome中,你可以试试这个

函数submitValue(){
var t=document.getElementById(“myTimer”).value;
var q=document.getElementById(“myQuestion”).value;
var b=document.getElementById(“myBounty”).value;
setItem(“#f38181”,JSON.stringify({t,q,b}))
location.href='index.html';

}
#f38181是我选择的css颜色。这不是我需要保存的东西<代码>var t=document.getElementById(“myTimer”).value;var q=document.getElementById(“myQuestion”).value;var b=document.getElementById(“myBounty”).value也不能保留。他们不允许我的代码进一步运行,因为index.html不包含div“myTimer”、“myQuestion”和“myBounty”。我不确定您是否理解我的问题,但感谢您建议将localStorage作为另一种尝试选项。然后使用固定键将数据存储在浏览器中(如上所述),然后在主html中检索数据。您需要两个单独的脚本来实现这一点。一个脚本在listing.html中,另一个脚本在index.html中。我该怎么做?我应该使用本地存储吗?在listing.html中是,首先将所有数据保存在本地存储中。将检索脚本放在index.html的document onload事件上以显示结果
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);
}