2

So basically I have two html pages in the same folder. One of them is the homepage, while the other is a page that basically is a form. Once the person fills out the form and clicks the submit button, I would like to make it so that it automatically changes the homepages information with the information written out on the form using DOM.

What I have tried:

  • Using an external & same JavaScript file for each HTML document, Firefox console said that the id is null
  • Using global variables, did not work.

If I haven't worded this well enough or if you don't understand, please comment and tell me!

Here's an example of what I tried to do, didn't work because the div with id type is in a different HTML document.

function submitform(){
    var textbox = document.getElementsByName('name').item(0);
    value= textbox.value;
    window.alert(value);
    document.getElementById('type').innerHTML = value;
}
4

2 回答 2

0

表格页面:

function submitform(){
    var textbox = document.getElementsByName('name')[0];
    value = textbox.value;
    localStorage["name"] = value;                      //save it in localStorage
}                                                      //for later use

主页:

function showStuff(){
    var value = localStorage["name"];                  //get the information back
    document.getElementById('type').innerHTML = value; //put it in
}

localStorage所有主要浏览器都支持。如果你需要支持<IE9,试试jStorage
尝试演示:http: //jsfiddle.net/DerekL/r4fXw/http://pastebin.com/SurbLhWZ


为什么你的尝试都不起作用

  1. 对每个 HTML 文档使用外部和相同的 JavaScript 文件,Firefox 控制台说 id 为空

    变量不会跨不同的网页共享。

  2. 使用全局变量,不起作用。

    与#1 相同。

于 2012-12-02T02:22:41.383 回答
0

将变量从另一个页面传递到一个页面需要某种形式的带有参数的查询,即。newpage.php?newdata='This came from the old page'. 您需要实现几个选项之一:如前所述,您可以将提交的数据存储在 cookie 中,然后在后续页面加载时检索它们,您可以使用实际的提交查询将数据发送回主页(见上文) 或者您可以使用 AJAX 例程将数据发送到主页,而无需任何类型的提交操作。

于 2012-12-02T02:26:01.397 回答