【问题标题】:How to avoid the custom entry in tagit() plugin如何避免 tagit() 插件中的自定义条目
【发布时间】:2013-10-23 01:42:40
【问题描述】:

在 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 也不起作用。

【问题讨论】:

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


    【解决方案1】:

    通过检查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;
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 2014-06-17
      • 2017-06-23
      相关资源
      最近更新 更多