HTML - 刷新后保留的输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1799899/
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
HTML - input value kept after Refresh
提问by JB Hurteaux
Usually, when a refresh is made on an HTML page, the values for input fields are kept (unless you do Ctrl+F5).
通常,在 HTML 页面上进行刷新时,输入字段的值会保留(除非您按 Ctrl+F5)。
Is there a header or other type of setting than can change this behavior, without chaging anything on the form or input itself?
是否有标题或其他类型的设置可以更改此行为,而无需更改表单或输入本身的任何内容?
I have a site where the input value is not kept after a page refresh in Production. However I do not have this behavior when I test this code on my local machine.
我有一个站点,在生产中刷新页面后不保留输入值。但是,当我在本地计算机上测试此代码时,我没有这种行为。
回答by McPherrinM
If you set the attribute autocomplete=off
, the content will never be stored.
如果你设置了属性autocomplete=off
,内容将永远不会被存储。
Alternatively, there's a plethora of ways to accomplish this with javascript, depending on exactly what you want to accomplish, whether it be clearing the whole form (use the reset() method), or resetting the single field.
或者,有很多方法可以使用 javascript 完成此操作,具体取决于您要完成的任务,是清除整个表单(使用 reset() 方法)还是重置单个字段。
回答by Mark Byers
You can add form.reset() to the body onload:
您可以将 form.reset() 添加到主体 onload:
<html>
<body onload="form1.reset();">
<form id="form1">
<textarea id="text"></textarea>
</form>
</body>
</html>
Update: while this might be a useful technique, I have now found out that it doesn't actually answer the submitter's question.
更新:虽然这可能是一种有用的技术,但我现在发现它实际上并没有回答提交者的问题。
回答by hex
How about setting a default value on DOM ready with javascript?
使用javascript在DOM上设置默认值怎么样?
Something like
就像是
<input name="foo" id="foo" type="text">
$('#foo').val('3')
回答by Mark Byers
As far as I know, there is no standard way to implement it, so before you can disable it, you first you need to determine how the site is doing it. There are many ways they could have done it, as you can see from the variety of different answers here.
据我所知,没有标准的方法来实现它,所以在禁用它之前,您首先需要确定站点是如何执行它的。他们有很多方法可以做到这一点,正如您从这里的各种不同答案中可以看出的那样。
A good way of understanding what is going on is to reduce the code to the simplest possible example that still reproduces the issue. Remove all graphics, unneeded text, style sheets and other formatting, irrelevant javascript, irrelevant HTML tags, etc. but always checking that you can still reproduce the issue. Eventually there will be so little code left that it should be obvious what it is that is causing the fields to be reset. You will have to do all this on the production machine, since you cannot reproduce it locally. To do this, take a copy of the scripts and rename them to index2.html, etc. Make sure you have backups of your production system before doing this, in case something goes wrong.
理解正在发生的事情的一个好方法是将代码简化为仍然重现问题的最简单的示例。删除所有图形、不需要的文本、样式表和其他格式、不相关的 javascript、不相关的 HTML 标签等,但始终检查您是否仍然可以重现问题。最终会留下如此少的代码,以至于导致字段被重置的原因应该很明显。您必须在生产机器上完成所有这些操作,因为您无法在本地复制它。为此,请复制脚本并将它们重命名为 index2.html 等。确保在执行此操作之前备份了生产系统,以防出现问题。
If you still can't understand how to fix the issue after doing this, the code should be sufficiently small that it can be posted here and someone else will be able to work it out.
如果您在执行此操作后仍然无法理解如何解决问题,那么代码应该足够小,以便可以将其发布在这里并且其他人将能够解决它。
回答by Xalsar
I normally use this:
我通常使用这个:
var reset_input_values = document.querySelectorAll('input');
for (var i = 0; i < reset_input_values.length; i++) {
reset_input_values[i].value = 'value you want ';
}
回答by SAI KAMAL DIDIGAM
I got this solved.
我解决了这个问题。
Work Around:
解决方法:
- Add a boolean flag to your typescript file and set disable(false) by default
- Place
*ngIf
on your html - On
ngAfterViewInit()
set that totrue
- 向您的打字稿文件添加一个布尔标志并默认设置禁用(假)
- 放置
*ngIf
在HTML - 在
ngAfterViewInit()
组到true