简体   繁体   English

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

[英]How to turn off autocomplete while keep using datalist element in html

I have a input field which shows a list using html5 <datalist> element.我有一个使用 html5 <datalist>元素显示列表的input字段。 The problem is that with <datalist> the browser autocomplete also shows the history list (which is the list of previously typed values, that are not included in the <datalist> ).问题在于,使用<datalist> ,浏览器自动完成功能还会显示历史列表(这是以前键入的值的列表,未包含在<datalist> )。 So I just want to get rid of the history-list not the <datalist> .所以我只想摆脱history-list而不是<datalist>

If I use the autocomplete = "off" feature, this also blocks the <datalist> .如果我使用autocomplete = "off"功能,这也会阻止<datalist>

In short, I just want the <datalist> not the history one.简而言之,我只想要<datalist>而不是历史记录。

Is it possible for you to use the input field without an id or name attribute?您是否可以使用没有idname属性的input字段? Without that, the browser doesn't really have any way to associate a history with that element.没有它,浏览器实际上没有任何方法将历史记录与该元素相关联。

In my real quick testing on Firefox, this seemed to do the trick:在我对 Firefox 的真正快速测试中,这似乎可以解决问题:

<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>

In the code above, the input s 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.在上面的代码中, input S采用的idname会记得过去的值,但input没有任何东西,只需输入class不会记得任何事情。

Unfortunately, this does make using the input slightly more difficult if you need it to have a name or id .不幸的是,如果您需要input nameid ,这确实会使使用input稍微困难一些。 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.在这种情况下,我会尝试使用id 'ed input ,它也是display: none 'ed 然后使用一些 JavaScript 使其与不会记住过去值的input保持同步。

Try using the HTML attribute autocomplete尝试使用 HTML 属性自动完成

<input name="q" type="text" autocomplete="off"/>

source Turn Off Autocomplete for Input source关闭输入的自动完成功能

I use a code like this:我使用这样的代码:

<input name="anrede" list="anrede" onmousedown="value = '';" />
<datalist id="anrede">
    <option value="Herr"></option>
    <option value="Frau"></option>
</datalist>

good : The datalist shown for selection is complete:显示供选择的数据列表已完成

bad : The input field is empty, therefore the old value is not documented, if needed to be remembered by the user bad :输入字段为空,因此旧值未记录,如果需要用户记住

you can use autocomplete="new-password".您可以使用自动完成=“新密码”。 autocomplete = "off" didn't worked in my case. autocomplete = "off" 在我的情况下不起作用。

<input name="q" type="text" autocomplete="new-password"/>

Try this, I hope it works.试试这个,我希望它有效。

<input id="datalisttestinput" list="stuff" autocomplete="off"></input>
<datalist id="stuff">
     <option id="1" value="Amy">
     <option id="2" value="Kristal">
     <option id="3" value="Collin">
     <option id="4" value="Carl">
</datalist>

try this:试试这个:

<datalist id="datalist_id">

js: js:

dataList = $("#datalist_id")
dataList.empty()

This will clear the datalist's history.这将清除数据列表的历史记录。 This worked for me on chrome.这在 chrome 上对我有用。 Then if you want to add options to the list, try:然后,如果您想向列表中添加选项,请尝试:

dataList.append("<option>Some Option</option>")

Cheers !!!干杯!!!

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

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