简体   繁体   English

多重选择字段(jQuery Select2插件)

[英]Multiple select field (Jquery Select2 plugin)

I am using Jquery Select2 plugin to create a multiple select-input field. 我正在使用Jquery Select2插件创建多个选择输入字段。 Similar to what is shown in the example on page https://select2.github.io/examples.html , I have created my code: 与页面https://select2.github.io/examples.html上的示例所示类似,我创建了代码:

    $(document).ready(function() {
        $("#category_tags___name___tagname").select2(
                {placeholder: "Select tags"},
        );

Now on server side I am using PHP framework(Symfony2) to create the HTML form, which looks roughly like this: 现在在服务器端,我正在使用PHP框架(Symfony2)创建HTML表单,该表单大致如下所示:

<select id="category_tags___name___tagname" name="category[tags][__name__][tagname][]" required="required" style="width:300px" multiple="" tabindex="-1" class="select2-hidden-accessible">
            <option value="1">tag1</option>
            <option value="2">tag2</option>
            <option value="3" selected="selected">tag3</option>
</select>

This works fine, on clicking the field, the available options appear in the dropdown list. 这很好,单击该字段后,可用选项会出现在下拉列表中。 But the problem is once I select any of the options, it gets added in the field as a tag, however it still appears in the dropdown list and is not disbaled ie. 但是问题是一旦我选择了任何一个选项,它就会作为标签添加到字段中,但是它仍然出现在下拉列表中,并且不会被屏蔽。 if I click that option again in the dropdown, it gets deselected. 如果我再次在下拉菜单中单击该选项,它将被取消选择。

I want the functionality that, once an option is selected it must appear in the field as a tag, but it should not appear in the dropdown list any longer. 我想要的功能是,一旦选择了一个选项,它就必须作为标签出现在字段中,但不再出现在下拉列表中。

I have searched the web for possible solutions. 我已经在网上搜索了可能的解决方案。 But found nothing so far. 但是到目前为止没有发现任何东西。

I am new to Jquery. 我是Jquery新手。 So all help is appreciated. 因此,感谢所有帮助。

According to the docs given in the ref site: https://select2.github.io/examples.html , One way to achieve what you're after would be to use select2:select and select2:unselect events to trigger you custom behavior: 根据参考站点中提供的文档: https : //select2.github.io/examples.html ,一种实现目标的方法是使用select2:selectselect2:unselect事件触发您的自定义行为:

try something like below: 尝试如下所示:

First in your html give each option value an specific ID something like: 首先在html中为每个选项值指定一个特定的ID ,例如:

   <option value="1" id='specific-1'>tag1</option>
   <option id='specific-2' value="2">tag2</option>
   <option value="3" id='specific-3' selected="selected">tag3</option>

the in your js: 在您的js中:

//when select is triggered
$("#category_tags___name___tagname").on("select2:select", function (e) {
   var myID = '#'+e.data.id //getting specific id from event
   $(myID).css('display','none') //hide it using css 
});

//when unselect is triggered
$("#category_tags___name___tagname").on("select2:unselect", function (e) {
   var myID = '#'+e.data.id //getting specific id from event
   $(myID).css('display','block') //show it using css 
});

PS : The above code is untested. PS :以上代码未经测试。 But, the logic seems to be right 但是,逻辑似乎是正确的

I prefer to send data to client side as json. 我更喜欢将数据作为json发送到客户端。 If I need to give an example 如果我需要举一个例子

data = "[{id:"",text:""},{id:"",text}]";

if you send data like above you can select them multiple and when chose any one it wont be shown again. 如果您像上面那样发送数据,则可以选择多个,并且在选择任何一个时都不会再次显示。

$('#id').select2('data', data);

and for your html page you can just create a textbox that is enough for select2 而对于您的html页面,您可以只创建一个足以供select2使用的文本框

<input type="text" id="id">

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

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