Javascript 使用javascript提交表单后如何保留表单数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2598904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:04:17  来源:igfitidea点击:

how do i keep form data after submitting the form using javascript?

javascriptforms

提问by bombo

When i submit this form, the values just disappears from the textboxes. I like them to stay printed in the textboxes. How do i do that?

当我提交此表单时,这些值会从文本框中消失。我喜欢它们留在文本框中打印。我怎么做?

<form id="myform" method="get" action="" onSubmit="hello();">

       <input id="hour" type="text" name="hour" style="width:30px; text-align:center;" /> :
       <input id="minute" type="text" name="minute" style="width:30px; text-align:center;" />
       <br/>
       <input type="submit" value="Validate!" />
    </form>

    <style type="text/css">
    .error {
        color: red;
        font: 10pt verdana;
        padding-left: 10px
    }
    </style>
<script type="text/javascript">
function hello(){

    var hour = $("#hour").html();
    alert(hour);
}
    $(function() {
        // validate contact form on keyup and submit
        $("#myform").validate({
            //set the rules for the fild names
            rules: {
                hour: {
                    required: true,
                    minlength: 1,
                    maxlength: 2,
                    range:[0,23]
                },
                minute: {
                    required: true,
                    minlength: 1,
                    maxlength: 2,
                    range:[0,60]
                },
            },
            //set messages to appear inline
            messages: {
                hour: "Please enter a valid hour",
                minute: "Please enter a valid minute"
            }
        });


    });
    </script>

采纳答案by T.J. Crowder

When you submit a form, the entire page is replaced with the response from the server. If you want to stay on the page (rather than having it replaced by the response), you might look at using jQuery.postor jQuery.ajaxto send the form data to the server rather than actually submitting the form.

当您提交表单时,整个页面将替换为来自服务器的响应。如果您想留在页面上(而不是将其替换为响应),您可能会考虑使用jQuery.postjQuery.ajax将表单数据发送到服务器,而不是实际提交表单。

回答by Aaron Digulla

As soon as you submit the page, the data is sent to the server and a new page is loaded. In your case, this is the same page as before but that doesn't make a difference for the browser. To keep the values, you must fill in the values on the serverwhile rendering the page.

一旦您提交页面,数据就会发送到服务器并加载一个新页面。在您的情况下,这是与以前相同的页面,但这对浏览器没有影响。要保留这些值,您必须在呈现页面时在服务器上填写这些值。

Usually, you can simply copy the data from the HTML request parameters into the fields.

通常,您可以简单地将数据从 HTML 请求参数复制到字段中。

回答by Ashwath Padmashali

In the form tag, include onsubmit ="return false"to avoid form reset. For example:

在表单标签中,包含onsubmit ="return false"以避免表单重置。例如:

<form name = "inputNumbers" onsubmit ="return false">

回答by Ashwath Padmashali

You can use cookies from JavaScript to keep values in. Basically you access something called document.cookie.

您可以使用 JavaScript 中的 cookie 来保存值。基本上,您可以访问名为document.cookie.

回答by B. Clay Shannon

As an example, to retain the data in a text input:

例如,要保留文本输入中的数据:

<input type="text" name="inputText" id="inputText" placeholder="Enter the name of your all-time favorite Duckbilled Platypus" value="@Request["inputText"]" autofocus style="display: inline-block;" />