且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在继续使用 html 中的 datalist 元素的同时关闭自动完成功能

更新时间:2023-08-26 16:20:04

是否可以在没有 idnameinput 字段?/code> 属性?没有它,浏览器就无法真正将历史记录与该元素相关联.

Is it possible for you to use the input field without an id or name attribute? Without that, the browser doesn't really have any way to associate a history with that element.

在我对 Firefox 的真正快速测试中,这似乎奏效了:

In my real quick testing on Firefox, this seemed to do the trick:

<form>
  <!-- these will remember input -->
  With id: <input id="myInputId" list="myList" /><br />
  With name: <input name="myInputName" list="myList" /><br />

  <!-- these will NOT remember input -->
  No id, name, class: <input list="myList" /><br />
  With class: <input class="myInputClass" list="myList" />

  <datalist id="myList">
    <option value="Option 1"></option>
    <option value="Option 2"></option>
  </datalist>

  <br />

  <input type="submit" />
</form>

在上面的代码中,带有 idnameinput 会记住过去的值,但 input 没有任何东西,只有一个 class 的输入将记住任何东西.

In the code above, the inputs with an id or name would remember past values, but the input without anything and the input with just a class would not remember anything.

不幸的是,如果您需要它具有nameid代码>.在这种情况下,我会尝试使用 id'ed input 这也是 display: none'ed 然后使用一些 JavaScript 来保持它与不会记住过去值的 input 同步.

Unfortunately, this does make using the input slightly more difficult if you need it to have a name or id. In that case, I'd try having an id'ed input which is also display: none'ed and then use some JavaScript to keep it in sync with an input that won't remember past values.