Javascript 页面重新加载时如何重置文本输入的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5502305/
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 17:29:43  来源:igfitidea点击:

How do I reset the value of a text input when the page reloads?

javascripthtml

提问by zjmiller

Firefox (and probably other browsers) want to keep whatever text the user entered in the text input, even after a reload. Just including the default text (that I want the input to revert to) in the html doesn't work:

Firefox(可能还有其他浏览器)希望保留用户在文本输入中输入的任何文本,即使在重新加载后也是如此。仅在 html 中包含默认文本(我希望输入恢复到)是行不通的:

<input tyep='text' value='default text' />

And neither does trying to use JS

尝试使用 JS 也不行

window.onload = function() {document.getElementById("mytextinput").value = 'default text'}

回答by JohnP

You can use plain old HTML :)

您可以使用普通的旧 HTML :)

Set autocomplete='off'in the attribute

autocomplete='off'在属性中设置

<input type='text' value='default text' autocomplete='off' />

This works on most modern browsers.

这适用于大多数现代浏览器。

回答by bpeterson76

Technically, you don't have to run a function onload to clear it--you could just put the javascript right in the page. For instance:

从技术上讲,您不必运行 onload 函数来清除它——您只需将 javascript 放在页面中即可。例如:

document.getElementById("mytextinput").value = ''

or with jQuery

或使用 jQuery

$("mytextinput").val('');

Note that it's always a good idea to work with a dom listener to ensure that your javascript fires after the dom has been properly built. So, in the example of jQuery, this would be as easy as

请注意,与 dom 侦听器一起使用以确保在正确构建 dom 后触发 javascript 始终是一个好主意。所以,在 jQuery 的例子中,这就像

$(document).ready(function() {
   $("mytextinput").val('');
});

回答by Tom Gullen

Try this:

尝试这个:

<input type='text' id='mytextinput' value='default text' />
<script>
    document.getElementById("mytextinput").value = 'default text';
</script>

This code should run as soon as the textbox has loaded. The onload event can seem inconsistent, I'm not entirely sure how it behaves in multiple browsers on different actions like refreshing.

此代码应在文本框加载后立即运行。onload 事件可能看起来不一致,我不完全确定它在多个浏览器中的不同操作(如刷新)中的行为。

You can call the script differently, put it in a function, put it somewhere else etc, but it's good to start with a working version.

您可以以不同的方式调用脚本,将其放在函数中,将其放在其他地方等,但最好从工作版本开始。

If this doesn't work on other browsers there isn't much you can do because some browsers will try and be 'helpful' and always autofill for you.

如果这在其他浏览器上不起作用,则您无能为力,因为某些浏览器会尝试“有用”并始终为您自动填充。

回答by nandu.com

I think you should do a server side coding on page reload. Page reload is generally a postback type idea . The page gets reloaded from server. If you are doing asp.net , in page_load method add your default text. :)

我认为您应该在页面重新加载时进行服务器端编码。页面重新加载通常是一种回发类型的想法。该页面从服务器重新加载。如果您正在做 asp.net ,请在 page_load 方法中添加您的默认文本。:)