如何在继续使用html中的datalist元素的同时关闭自动完成功能 [英] How to turn off autocomplete while keep using datalist element in html

查看:105
本文介绍了如何在继续使用html中的datalist元素的同时关闭自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个input字段,该字段显示了使用html5 <datalist>元素的列表.问题在于,使用<datalist>时,浏览器的自动完成功能还会显示历史记录列表(这是先前键入的值的列表,这些值未包含在<datalist>中).所以我只想摆脱history-list而不是<datalist>.

如果我使用autocomplete = "off"功能,这也会阻止<datalist>.

简而言之,我只想要<datalist>而不是历史记录.

解决方案

是否可以使用没有idname属性的input字段?否则,浏览器实际上将无法将历史记录与该元素相关联.

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

在上面的代码中,带有idnameinput会记住过去的值,但是没有任何内容的input和仅带有class的输入将不会记住任何事情.

不幸的是,如果 需要nameid,使用input确实会更加困难.在这种情况下,我尝试使用id'ed input也是display: none'ed,然后使用一些JavaScript使其与不会记住过去值的input保持同步./p>

I have a input field which shows a list using html5 <datalist> element. 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>). So I just want to get rid of the history-list not the <datalist>.

If I use the autocomplete = "off" feature, this also blocks the <datalist>.

In short, I just want the <datalist> not the history one.

解决方案

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.

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>

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.

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.

这篇关于如何在继续使用html中的datalist元素的同时关闭自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆