【问题标题】:jQuery tag-it only allow available tagsjQuery标签-它只允许可用的标签
【发布时间】:2013-12-08 09:27:14
【问题描述】:

我在我的脚本中使用 jQuery tag-it

$("#user_autocomplete").tagit({
    tagSource: function (request, response) {
        $.ajax({
            data: {
                term: request.term
            },
            type: "POST",
            url: "/site2/message/users.php",
            dataType: "json",
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item,
                        value: item
                    }
                }));
            }
        });
    },
    tagLimit: 3,
    autocomplete: {
        delay: 0,
        minLength: 1
    },
});

在这种情况下,确认所有输入字段。但我只想要可添加的字段。怎么做?

【问题讨论】:

  • 你能解释更多吗? “只有可用的字段”是什么意思?
  • 感谢您添加自己的解决方案。我已将其移至答案;如果您希望将来为自己的问题提供解决方案,最好像这样自我回答。

标签: jquery tag-it


【解决方案1】:

代表 OP 发布:

我正在使用 beforeTagAdded 函数我找到了答案:

        tagSource: function(request, response) 
        {
            $.ajax({
                data: { term:request.term },
                type: "POST",
                url:        "/site2/message/users.php",
                dataType:   "json",
                 success: function( data ) {
                     array = data;
                    response( $.map( data, function( item ) {

                        return {
                            label:item,
                            value: item
                        }
                        }));
                    }

            });
            },
        tagLimit :3,
        autocomplete: {delay: 0, minLength: 1},
        beforeTagAdded: function(event, ui) {
            if(array.indexOf(ui.tagLabel) == -1)
            {
                return false;
            }
            if(ui.tagLabel == "not found")
            {
                return false;
            }

        },

【讨论】:

  • @Kakitori:我只是代表 OP 发布了它 - 如果您喜欢原始帖子,也许可以考虑投票?
  • 感谢您分享这片幸福(确实为原帖投了赞成票;))!您应该考虑向原始项目提出拉取请求:)
【解决方案2】:

我可以通过在 createTag 函数的顶部添加以下检查来实现这一点:

if (this.options.onlyAvailableTags && $.inArray(this._formatStr(value), this.options.availableTags) == -1) {
    return false;
}

然后在tag-it 调用中添加此选项:

onlyAvailableTags: true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多