jQuery 使文本输入字段记住以前输入的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16444496/
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
Make text input fields remember previously entered data
提问by 1252748
My text inputs seem not to remember values that have been typed before. For example, many websites that I don't even have an account on, but have, for example entered my email address before (buying a train ticket as a "guest") give me a sort of dropdown with email addresses I've used in the past. Yet my form does not do this. It obliges me to type it out completely every time. Seems to be the opposite of this question..
我的文本输入似乎不记得之前输入的值。例如,许多网站我什至没有帐户,但例如之前输入了我的电子邮件地址(作为“客人”购买火车票)给了我一种包含我使用过的电子邮件地址的下拉列表在过去。然而我的表格并没有这样做。它迫使我每次都把它完整地打出来。似乎与这个问题相反..
I have simple inputs like <input type="text" id="firstName" placeholder="First name" />
我有简单的输入,如 <input type="text" id="firstName" placeholder="First name" />
And they are submitted with simple jquery ajax post. Something like this. However the ajax isn't working on jsbin on that example, I just show it to demonstrate my basic structure. Is there a jquery plugin for this? Any way I can control this seemingly browser-driven behavior. Thanks.
它们是通过简单的 jquery ajax post 提交的。像这样的东西。但是,ajax 不适用于该示例中的 jsbin,我只是展示它以演示我的基本结构。是否有用于此的 jquery 插件?我可以通过任何方式控制这种看似浏览器驱动的行为。谢谢。
回答by geobalas
In most browsers, you can make your browser display values previously entered in input fields by giving a name to the input. For example:
在大多数浏览器中,您可以通过为输入命名,使浏览器显示先前在输入字段中输入的值。例如:
<input type="text" id="firstName" placeholder="First name" name="firstName" >
If the field has no name attribute, the browser won't show suggestions.
如果该字段没有 name 属性,浏览器将不会显示建议。
Also check if javascript prevents the default action on submit.
还要检查 javascript 是否阻止了提交时的默认操作。
回答by Jeff Moretti
I had the same problem. For me, it was when developing in ASP.NET MVC. Anyways, the solution I had to make was a combination of things:
我有同样的问题。对我来说,是在 ASP.NET MVC 中开发时。无论如何,我必须做出的解决方案是综合考虑:
1) Having the name attribute defined in my input tags
1) 在我的输入标签中定义了 name 属性
2) Having autocomplete="on"
in my input tags
2)autocomplete="on"
在我的输入标签中
3) Changing the button tag to be a type="submit"
tag
3)将按钮标签更改为type="submit"
标签
4) Enclosing all my input controls in a <form>
tag
4) 将我所有的输入控件包含在一个<form>
标签中
Give this a try if you are stuck, hopefully it will help you out :)
如果您遇到困难,请尝试一下,希望它可以帮助您:)
Cheers,
干杯,
Jeff Moretti
杰夫·莫雷蒂
回答by Andrei Nemes
It may vary depending on browser and user privacy settings and sorts, and it should be on by default, but try adding autocomplete="on"
to your input tag.
它可能因浏览器和用户隐私设置和种类而异,默认情况下它应该是打开的,但请尝试添加autocomplete="on"
到您的输入标签。
回答by Nguyen
Add attribute autocomplete="off" to your input
将属性 autocomplete="off" 添加到您的输入
回答by mmohiudd
You may try autocomplete="on"
你可以试试 autocomplete="on"
<input type="text" id="firstName" placeholder="First name" autocomplete="on" />