如何避免tagit()插件中的自定义条目

时间:2013-10-15 09:48:36

标签: jquery autocomplete custom-controls ruby-on-rails-4 tag-it

在rails 4.0中,我试图使用jquery tagit()插件实现标签输入字段。 在此插件中,用户应从自动填充列表中选择输入标签而不是自定义条目。在这里我该如何避免自定义条目?而且在这里,我提到minLength为2,但是当我输入第一个字母时,自动填充列表会显示。

对于代码参考,我使用了https://github.com/aehlke/tag-it

代码是,

jQuery(document).ready(function() {
 jQuery("#DIV_USERNAME").tagit({
   minLength: 2,
   tagLimit: 3,
   allowNewTags: false,
   placeholderText: "You can enter only 3 tags",
   tagSource: function( request, response ) {
     $.ajax({
       url: "autocomplete/names",
       data: { term:request.term },
       dataType: "json",
       success: function( data ) {
         response( $.map( data, function( item ) {
           return {
             label: item.value
           }
         }));
       }
     });
   }
 });
});

如果我提到allowNewTags:false,它也无效。

1 个答案:

答案 0 :(得分:0)

从检查tagit源代码来看,该选项看起来不可用。

我会在这里做他们所说的话:https://stackoverflow.com/a/30495652

var tags_list = ['tag1', 'tag2', 'tag3'];

$("input[name='subject-tags']").tagit({
    availableTags : tags_list,
    beforeTagAdded : function(event, ui) {
        if(tags_list.indexOf(ui.tagLabel) == -1){
            return false;
        }
    }
});