简体   繁体   English

如何在输入字段中添加多个值

[英]How to add multiple values in input field

I am trying to add multiple names from the autocomplete drop-down to the input field like in the image below.我正在尝试将多个名称从自动完成下拉列表添加到输入字段,如下图所示。

在此处输入图像描述

Here's what I want to build:这是我想要构建的:

  1. When I click on any item from the drop-down, it would add the item in the input field exactly how it is shown in the image.当我从下拉列表中单击任何项目时,它会在输入字段中添加该项目,这与它在图像中的显示方式完全相同。
  2. When I hit backspace in the input field, the entire name should be removed at once.当我在输入字段中按退格键时,应立即删除整个名称。
  3. I can add multiple names and send them to sit in the input field我可以添加多个名称并将它们发送到输入字段中

So basically the name should sit inside that box and that box should sit inside the input field.所以基本上名字应该放在那个盒子里面,那个盒子应该放在输入字段里面。 How do I achieve that in Javascript?如何在 Javascript 中实现这一点? I tried searching the web for this but I am not sure if I am putting the right keyword for it.我尝试为此搜索 web,但我不确定我是否为它输入了正确的关键字。 I don't know what it is called.我不知道它叫什么。

One way to do it is:一种方法是:

<div>
  <div role="button">Eight</div>
  <input />
</div>

You have a container, inside of the container you dynamically insert the selected options first (the inner div s) and then render an input which takes the rest of the space.您有一个容器,在容器内您首先动态插入所选选项(内部div s),然后呈现一个输入,该输入采用空间的 rest。 Also you should render a Menu which handles the list for you.此外,您应该呈现一个为您处理列表的菜单。

  1. You do it using javascript.您可以使用 javascript 来完成。 You should know which value has been clicked on add add an element inside your container.您应该知道单击了哪个值添加在容器内添加元素。 (You can use a function to get the label of the options) (您可以使用 function 获得选项的 label)

  2. One way is to use listen to the key events in your input and... .一种方法是使用侦听输入中的关键事件和...。

  3. I think if you render other options outside of the input would be better (and easier).我认为如果您在输入之外呈现其他选项会更好(并且更容易)。

I suggest to take a look at material-ui Autocomplete and inspect it using your browser or if you like take a look at the code in the github.我建议查看material-ui Autocomplete并使用您的浏览器检查它,或者如果您喜欢查看 github 中的代码。 (Or other libraries which have already implemented this) (或其他已经实现的库)

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

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