简体   繁体   English

在jquery select2中标记文本

[英]Tagging text in jquery select2

<select multiple id="e1" style="width:300px">
        <option value="AL">Alabama</option>
        <option value="Am">Amalapuram</option>
        <option value="An">Anakapalli</option>
        <option value="Ak">Akkayapalem</option>
        <option value="WY">Wyoming</option>
    </select>

$("#e1").select2({'placeholder': ' ','allowClear': true});

it is working fine for matched text. 它适用于匹配的文本。 But i want to tag text(which doesnot match any option). 但我想标记文本(与任何选项都不匹配)。 Kindly help. 请帮助。

please find the demo code in following link. 请在以下链接中找到演示代码。 http://jsfiddle.net/marcusasplund/jEADR/2/ http://jsfiddle.net/marcusasplund/jEADR/2/

You have the tags option, but it can't be used with select element 您有标签选项,但它不能与select元素一起使用

 $("#e1").select2({ tags: [{ "id": "AL", "text": "Alabama" }, { "id": "Am", "text": "Amalapuram" }, { "id": "An", "text": "Anakapalli" }, { "id": "Ak", "text": "Akkayapalem" }, { "id": "WY", "text": "Wyoming" }] }); 
 <script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.css"/> <input id="e1" style="width:300px" /> 

There is an option called tags in select2 which allows you to unmatched text to tags but there are some problems here. select2有一个名为tags的选项,它允许您将不匹配的文本添加到tags但这里存在一些问题。

  • It will not allow you to set tags option if you are binding select2 plugin on select . 如果您在select上绑定select2插件,它将不允许您设置tags选项。 So for this you need to bind select2 on input . 因此,您需要在input上绑定select2

SOURCE QUESTION 消息来源问题

  • Select2 Version 4.0 allows you to add tags option to select control too. Select2 Version 4.0允许您添加tags选项以select控件。

So the solution is either you need to upgrade to the latest plugin or you need to bind select2 on input control. 因此,解决方案是您需要升级到最新的插件,或者您需要在input控件上绑定select2

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

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