JavaScript 在提交 HTML 表单时显示新页面

标签 javascript html

假设我有 2 个 html 文件:form.html 和 confirm.html

form.html 只有一个文本字段和一个提交按钮,当您点击提交时,它会显示您刚刚在文本字段中输入的内容。

    <HTML>
      <HEAD>
        <TITLE>Test</TITLE>
        <script type="text/javascript"> 

        function display(){ 
            document.write("You entered: " + document.myform.data.value);
        } 
        </script> 
      </HEAD>
      <BODY>
      <center>    
<form name="myform">

  <input  type="text" name="data">
  <input type="submit" value="Submit" onClick="display()">

</form>
    </BODY>
    </HTML>

现在我希望当点击提交按钮时它会在 confirm.html 中显示输入的值。我应该怎么办?我的意思是 confirm.html 中应该包含什么以及如何在其他位置使用 form.html 中的数据,我是否需要创建一个单独的 JavaScript 文件来存储 JS 函数,以便我可以在两个 html 文件中使用它。我对各种东西都很陌生。

注意:这里没有 PHP 或服务器端语言或任何 super 语言,我的桌面上只有 2 个 html 文件,我想使用 FireFox 进行测试。

谢谢!

最佳答案

您可以尝试使用 localStoragecookies。检查下面找到的 2 个解决方案之一...

1 - 如果您有 HTML5,您可以将您输入的内容存储到 localStorage

试试这个例子:

form.html:

<html>
<head>
    <title>Test</title>
    <script type="text/javascript">

        // Called on form's `onsubmit`
        function tosubmit() {
            // Getting the value of your text input
            var mytext = document.getElementById("mytext").value;

            // Storing the value above into localStorage
            localStorage.setItem("mytext", mytext);

            return true;
        }

    </script> 
</head>
<body>
    <center>   
        <!-- INLCUDING `ONSUBMIT` EVENT + ACTION URL --> 
        <form name="myform" onsubmit="tosubmit();" action="confirm.html">

            <input  id="mytext" type="text" name="data">
            <input type="submit" value="Submit">

        </form>
</body>
</html>


confirm.html:

<html>
<head>
<script>

    // Called on body's `onload` event
    function init() {
        // Retrieving the text input's value which was stored into localStorage
        var mytext = localStorage.getItem("mytext");

        // Writing the value in the document
        document.write("passed value = "+mytext);
    }

</script>
</head>    

<body onload="init();">

</body>

</html>

2 -另外,正如@apprentice 提到的,您还可以使用符合 HTML 标准的 cookie。

试试这个例子:

form.html:

<html>
<head>
    <title>Test</title>
    <script type="text/javascript">
        // Function for storing to cookie
        function setCookie(c_name,value,exdays)
        {
            var exdate=new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
            document.cookie=c_name + "=" + c_value;
        }

        // Called on form's `onsubmit`
        function tosubmit() {
            // Getting the value of your text input
            var mytext = document.getElementById("mytext").value;

            // Storing the value above into a cookie
            setCookie("mytext", mytext, 300);

            return true;
        }

    </script> 
</head>
<body>
    <center>   
        <!-- INLCUDING `ONSUBMIT` EVENT + ACTION URL --> 
        <form name="myform" onsubmit="tosubmit();" action="confirm.html">

            <input  id="mytext" type="text" name="data">
            <input type="submit" value="Submit">

        </form>
</body>
</html>


confirm.html:

<html>
<head>
<script>

    // Function for retrieveing value from a cookie
    function getCookie(c_name)
    {
        var i,x,y,ARRcookies=document.cookie.split(";");
        for (i=0;i<ARRcookies.length;i++)
        {
            x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
            y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
            x=x.replace(/^\s+|\s+$/g,"");
            if (x==c_name)
            {
                return unescape(y);
            }
        }
    }

    // Called on body's `onload` event
    function init() {
        // Retrieving the text input's value which was stored into a cookie
        var mytext = getCookie("mytext");

        // Writing the value in the document
        document.write("passed value = "+mytext);
    }

</script>
</head>    

<body onload="init();">

</body>

</html>

关于JavaScript 在提交 HTML 表单时显示新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12887702/

相关文章:

html - 如何使用HTML5或WEBRTC在浏览器中发送UDP包?

javascript - 是否有正确的方法来解析 JavaScript 中以循环开头的 JSON?

javascript - jQuery $(window).blur 与 native window.onblur

javascript - 在 Node.js 中实现 JSON Web 加密

javascript - 单独的 JS 文件中的 Angular 指令?

html - 淡化动态背景图像?

php - 屏蔽状态栏中的 URL

javascript - jQuery 固定侧边栏在溢出时随内容移动

jquery - 在 React 组件中应用 sass css 时遇到问题

html - 为什么在提供 flex-basis 时 flex 元素不会转到下一行?