简体   繁体   English

使文本输入字段记住以前输入的数据

[英]Make text input fields remember previously entered data

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.例如,许多我什至没有帐户的网站,例如之前输入我的 email 地址(作为“客人”购买火车票)给我一个下拉列表,其中包含我使用过的 email 地址在过去。 Yet my form does not do this.但是我的表格没有这样做。 It obliges me to type it out completely every time.它迫使我每次都把它完整地打出来。 It 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 a jQuery Ajax post.他们提交了简单的 jQuery Ajax 帖子。 Something like this .这样的东西。 However the Ajax isn't working on jsbin on that example, I just show it to demonstrate my basic structure.然而 Ajax 并没有在那个例子的 jsbin 上工作,我只是展示它来展示我的基本结构。 Is there a jQuery plugin for this?这个有jQuery插件吗? Is there a way I can control this seemingly browser-driven behavior?有什么方法可以控制这种看似浏览器驱动的行为吗?

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 是否阻止了提交时的默认操作。

I had the same problem.我有同样的问题。 For me, it was when developing in ASP.NET MVC.对我来说,是在 ASP.NET MVC 中开发时。 Anyway, the solution I had to make was a combination of things:无论如何,我必须做出的解决方案是以下几点的组合:

  1. Having the name attribute defined in my input tags在我的输入标签中定义了 name 属性

  2. Having autocomplete="on" in my input tags在我的输入标签中有autocomplete="on"

  3. Changing the button tag to be a type="submit" tag将按钮标签更改为type="submit"标签

  4. Enclosing all my input controls in a <form> tag将所有输入控件包含在<form>标记中

它可能会因浏览器和用户隐私设置和排序而异,默认情况下它应该是打开的,但请尝试将autocomplete="on"添加到您的输入标签。

some browsers adamantly refused to remember an input until i "remembered it manually" via (ab)using localStorage... this worked for me:一些浏览器坚决拒绝记住输入,直到我通过(ab)使用 localStorage “手动记住它”......这对我有用:

    {
        // hack to remember last error id on page refresh
        // can't believe i have to do this, but my browser don't want to remember it itself, 
        // ... not sure why
        let errorid_element=document.querySelector("#errorlog_id");
        let rememberHack=function(){
            localStorage.setItem("last_id_remember_hack",errorid_element.value);
        };
        errorid_element.addEventListener("input",rememberHack);
        errorid_element.addEventListener("change",rememberHack);
        errorid_element.value = localStorage.getItem("last_id_remember_hack");
    }

In bootstrap you would fill up the for='' field在引导程序中,您将填写 for='' 字段

Example:例子:

    <div class="custom-control custom-checkbox checkbox-lg">
      <input type="checkbox" class="custom-control-input" id="TER" value="TER">
      <label class="custom-control-label" for="TER">Land/Lot</label>
    </div>

^ Notice how for and id values match ^ 注意forid值是如何匹配的

If you are using React如果你正在使用 React

  • give each input a name给每个输入一个名字
  • wrap in <form>包裹在<form>
  • prevent default form action防止默认表单操作
  • add type="submit" on any submission buttons, and do not include onClick .在任何提交按钮上添加type="submit" ,并且不包括onClick Let the onSubmit callback handle that.onSubmit回调处理。

Now hitting enter or clicking the button will remember the value entered.现在按回车键或单击按钮将记住输入的值。

<form onSubmit={(e) => {
        e.preventDefault(); // prevent new page from being opened
        // do something with input data
     }
}>
    <input name="myInput" ... />
    <button type="submit" ... />
</form>

https://reactjs.org/docs/forms.html https://reactjs.org/docs/forms.html

The below worked well for me.以下对我来说效果很好。 I'm copying and pasting the chunks that matter so you may need to tweak it slightly to get it to work for you but it should largely "just work".我正在复制和粘贴重要的块,因此您可能需要稍微调整一下才能让它为您工作,但它应该在很大程度上“正常工作”。

$(function(){
  function save_user_inputs(ev){
    $("input").each(function(index, $input){
      if ($input.name == ""){
        return true;
      }       
      localStorage.setItem("input." + $input.name, $input.value);
    }); 
    return true;
  } 

  function load_user_inputs(){
    var input_names = Object.keys(localStorage);
    for(var i=0; i<input_names.length; i++){
      var input_name = input_names[i];
      if (!input_name.startsWith("input.")){
        continue;
      }   
      var $input = $("[name='"+input_name.split(".")[1]+"']")
      $input.val(localStorage[input_name]);
    }   
  } 
        
  $(document).on('submit', save_user_inputs);
  load_user_inputs();
});

您可以尝试autocomplete="on"

<input type="text" id="firstName" placeholder="First name" autocomplete="on" />

将属性autocomplete="off"添加到您的输入中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM