简体   繁体   English

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

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

相关问题 如何使用 document.getElementById(“abc”).innerHTML DOM 以字符串格式显示 JavaScript object 的内容? - How do I display the content of a JavaScript object in a string format using document.getElementById(“abc”).innerHTML DOM? 如何使用模态输入中的document.getElementById? - How do I use document.getElementById from an input in a modal? 如何使用document.getElementById(&#39;elementID&#39;)从JavaScript中的纸张输入元素获取值 - How to get get the value from a paper input element in JavaScript using document.getElementById('elementID') 如何使用document.getElementbyID(&#39;&#39;)。innerHTML将文本从函数链接到同一网页? - How to link text from a function to the same webpage using document.getElementbyID('').innerHTML? 使用document.getElementByID从文本输入中获取值不起作用 - Getting value from text input using document.getElementByID not working 使用document.getElementById显示数组中的多个图像 - Display multiple images from array using document.getElementById 使用document.getElementById - using document.getElementById 如何显示我的价值? 使用 document.getElementById - How to display my value? using document.getElementById 通过document.getElementbyid()使用javascript进行用户名验证 - username validation using javascript by document.getElementbyid() 如何检测用户单击或使用document.getElementById的javascript代码所做的复选框更改 - How to detect checkbox change made by user click or javascript code using document.getElementById
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM