简体   繁体   English

将id添加到select2输入文本

[英]add id to select2 input text

is it possible to add an id to the input type text that select2 is generating? 是否可以在select2生成的输入类型文本中添加ID? to explain what i want, i have to make a filter based on cities, since there are too many cities, i cant load them all at once, so i made an autocomplete, but the user can choose more cities, so i thought about an autocomplete with select2, i tried using the class, like this 为了解释我想要的内容,我必须基于城市创建一个过滤器,因为城市太多,我无法一次加载所有城市,因此我完成了自动填充,但是用户可以选择更多城市,因此我想到了使用select2自动完成,我尝试使用该类,像这样

$('.select2-search__field').on('keyup', function(e) {
                console.log($(this).val());
            }); 

The problem is, i have more select2 on my page (one with some codes, one with some regions etc.), and i would do useless ajax requests for each of them 问题是,我在页面上有更多的select2(一个带有一些代码,一个带有一些区域等),并且我会对每个页面执行无用的ajax请求

Let's think outside the box. 让我们跳出框框思考。 You might not have to put an ID on a Select2 instance. 您可能没有把一个ID上选择二实例。

You need to somehow differentiate between different instances of Select2. 您需要以某种方式区分Select2的不同实例。 Maybe an easier approach, which you're already familiar with, would be to wrap them in a div which has an ID: 也许您已经熟悉了一种更简单的方法,那就是将它们包装在具有ID的div中:

<div id=first>
  <!-- one select2 -->
</div>
<div id=second>
  <!-- the other select2-->
</div>

You can now access different ones based on the ID of the wrapper. 现在,您可以根据包装器的ID访问不同的包装器。

$('#first .select2-search__field')

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

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