如何使用javascript将值从一个html页面传递到另一个html页面 [英] How can I pass values from one html page to another html page using javascript

查看:298
本文介绍了如何使用javascript将值从一个html页面传递到另一个html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第一页即时获取价值的文本框中,我需要将它传递到另一个页面,分为2个框架。
我需要在第一帧的html页面显示该值。
请给我一个简单的例子。
我尝试了
window.document.getElementById(inputbox1)。value
但我无法获取该值。

请给我一个简单的例子。

你可以使用纯JavaScript来实现这一点。您在表单页面上会显示如下内容:

 < form action =param-received.htmlmethod = GET> 
< input type =textid =fooname =foo>
< input type =submitvalue =Sendname =submitid =submit>
< / form>

点击发送按钮后,您将被重定向到 / param- received.html?foo = hola& submit =发送




  • location.search 属性包含参数链。

  • ?连接URL和参数字符串。

  • &分隔多个参数。
  • =为变量赋值。


    这里是完整的处理在 param-received.html 上发送的数据的代码:

     << ; script language =JavaScript> 
    函数processForm()
    {
    var parameters = location.search.substring(1).split(&);
    var temp = parameters [0] .split(=);
    l = unescape(temp [1]);
    alert(l); //将文本放在文本框中的对话框
    }
    processForm();
    < / script>


    In first page im getting value in textbox i need to pass it to another page which is divided into 2 frames. I need to display that value in first frame's html page. Please provide me a simple example. I tried with window.document.getElementById("inputbox1").value but im unable to get the value.

    Please provide me a simple example.

    解决方案

    I would go with localStorage, as @MicrosoftGoogle propose, but is not well supported yet, you can use pure javascript to achieve this. You will have something like this on your form page:

    <form action="param-received.html" method="GET">
       <input type="text" id="foo" name="foo">
       <input type="submit" value="Send" name="submit" id="submit">
    </form>
    

    Once you click on Send button,you will be redirect to /param-received.html?foo=hola&submit=Send.

    • location.search attribute contains the chain of parameters.
    • ? concatenates the URL and the string of parameters.
    • & separates multiple parameters.
    • = assigns a value to the variable.

    Here is the complete code to process data sent on param-received.html:

    <script language="JavaScript">
      function processForm()
      {
        var parameters = location.search.substring(1).split("&");
        var temp = parameters[0].split("=");
        l = unescape(temp[1]);
        alert(l); //Dialog with the text you put on the textbox
      }
      processForm();
    </script>
    

    这篇关于如何使用javascript将值从一个html页面传递到另一个html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆