Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

javascript - 清除页面刷新上的输入字段(Microsoft Edge)(clear input fields on page refresh (Microsoft Edge))

I'm just a beginner, but can someone help me out?

(我只是一个初学者,但是有人可以帮我吗?)

I have an HTML page with a couple of input fields, and I want the fields to be cleared when the page is refreshed.

(我有一个包含两个输入字段的HTML页面,并且希望刷新页面时清除这些字段。)

My HTML looks like this:

(我的HTML看起来像这样:)

<input type='text' id='input_field' value=''>

Then, toward the beginning of my script, there's this:

(然后,在我的脚本开始处,有以下内容:)

document.getElementById('input_field').value = '';

This works fine in Firefox;

(在Firefox中工作正常;)

when the page is refreshed, the value is emptied and the field is cleared.

(刷新页面时,将清空该值并清除该字段。)

It does not work in Microsoft Edge;

(它在Microsoft Edge中不起作用。)

when the page is refreshed, the actual value is emptied, but the contents of the field remain.

(刷新页面时,将清空实际值,但该字段的内容仍保留。)

I've also tried wrapping my input element(s) in a form element, and using JavaScript to reset the form, but the results are the same.

(我也尝试过将输入元素包装在一个form元素中,并使用JavaScript重置该表单,但是结果是相同的。)

As you can see, I've kept the value attribute in the HTML, but only for purposes of demonstration and experimentation.

(如您所见,我将value属性保留在HTML中,但仅用于演示和实验目的。)

Omitting it has no effect in either browser.

(忽略它在任一浏览器中均无效。)

However, if I leave the HTML attribute but omit the JavaScript statement, both browsers behave differently: in Edge, the contents of the field "catch up" to the actual value (ie, the field clears), but only after the page is refreshed twice;

(但是,如果我保留HTML属性而忽略JavaScript语句,则两种浏览器的行为都不同:在Edge中,仅在刷新页面后,字段的内容才会“赶上”到实际值(即,该字段将清除)。两次;)

and in Firefox, this fails to update the value at all, no matter how many times the page is refreshed.

(在Firefox中,无论刷新页面多少次,这都根本无法更新值。)

Finally, if I edit the source in any way and then reload the page in Edge, it empties the actual value and also clears the field, but only on that initial reload.

(最后,如果我以任何方式编辑源代码,然后在Edge中重新加载页面,它将清空实际值,并清除该字段,但仅限于该初始重新加载。)

After that, the problem persists.

(之后,问题仍然存在。)

This makes me wonder if it's a cache issue.

(这使我想知道这是否是缓存问题。)

Any help or suggestions?

(有什么帮助或建议吗?)

  ask by TeRrIrAoLr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add the attribute autocomplete="off" to the <input> tag.

(将属性autocomplete="off"<input>标记。)

<input autocomplete="off">

This may not work on all browsers.

(这可能不适用于所有浏览器。)

See this site for browser support chart: https://caniuse.com/#feat=input-autocomplete-onoff

(请参阅此网站以获取浏览器支持图表: https : //caniuse.com/#feat=input-autocomplete-onoff)

If it's not working on Edge try the solution provided on this fiddle by fizzix.

(如果在Edge上不起作用,请尝试fizzix在此提琴上提供的解决方案。)

Original post Chrome Browser Ignoring AutoComplete=Off .

(Chrome浏览器忽略AutoComplete = Off的原始帖子。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...