简体   繁体   English

单击时清除输入值并重置数据列表选项下拉列表

[英]Clear a input value and also reset the datalist option dropdown when clicked

I created an input that has a datalist with some options, like this:我创建了一个输入,其中包含一个带有一些选项的数据列表,如下所示:

<div>
  <input id="fruitsInput" type="text" list="fruits" placeholder="choose a fruit" >
  <datalist id="fruits">
    <option value="banana">  
    <option value="apple">      
    <option value="pear">    
    <option value="strawberrie">
  </datalist>
</div>

When the input has a value, for example "apple", and the user clicks on the input again I want to clear the value so the user can select another fruit.当输入有一个值时,例如“apple”,并且用户再次单击输入我想清除该值,以便用户可以 select 另一个水果。 So I did this with vanilla Js所以我用香草Js做了这个

const input = document.getElementById("fruitsInput");

input.addEventListener('click', () => {
    if(input.value) {
    input.value = "";
  }
});

It is working, but when I click on the input and it clears the value, the dropdown list with the options only shows the last value that the user selected.它正在工作,但是当我单击输入并清除值时,带有选项的下拉列表仅显示用户选择的最后一个值。 In that apple example, only apple will appear as a option when the user clicks again to clear the value.在那个 apple 示例中,当用户再次单击以清除该值时,只有 apple 将作为选项出现。

在此处输入图像描述

Here's an live example on JSFiddle: https://jsfiddle.net/d8fas05u/14/这是 JSFiddle 上的一个实时示例: https://jsfiddle.net/d8fas05u/14/

I want all the options to be shown again.我希望再次显示所有选项。 Is there a way to do it?有没有办法做到这一点?

I created an input that has a datalist with some options, like this:我创建了一个输入,其中包含一个带有一些选项的数据列表,如下所示:

<div>
  <input id="fruitsInput" type="text" list="fruits" placeholder="choose a fruit" >
  <datalist id="fruits">
    <option value="banana">  
    <option value="apple">      
    <option value="pear">    
    <option value="strawberrie">
  </datalist>
</div>

When the input has a value, for example "apple", and the user clicks on the input again I want to clear the value so the user can select another fruit.当输入有一个值时,例如“apple”,并且用户再次单击输入我想清除该值,以便用户可以 select 另一个水果。 So I did this with vanilla Js所以我用香草Js做了这个

const input = document.getElementById("fruitsInput");

input.addEventListener('click', () => {
    if(input.value) {
    input.value = "";
  }
});

It is working, but when I click on the input and it clears the value, the dropdown list with the options only shows the last value that the user selected.它正在工作,但是当我单击输入并清除值时,带有选项的下拉列表仅显示用户选择的最后一个值。 In that apple example, only apple will appear as a option when the user clicks again to clear the value.在那个 apple 示例中,当用户再次单击以清除该值时,只有 apple 将作为选项出现。

在此处输入图像描述

Here's an live example on JSFiddle: https://jsfiddle.net/d8fas05u/14/这是 JSFiddle 上的一个实时示例: https://jsfiddle.net/d8fas05u/14/

I want all the options to be shown again.我希望再次显示所有选项。 Is there a way to do it?有没有办法做到这一点?

You should click on button你应该点击按钮在此处输入图像描述 . .

Because if you click on space area, maybe I think it display history of input value.因为如果您单击空间区域,也许我认为它会显示输入值的历史记录。 This means live code is correct.这意味着实时代码是正确的。

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

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