jQuery标签 - 它只允许可用的标签

时间:2022-10-27 12:57:03

i use jQuery tag-it in my script:

我在我的脚本中使用jQuery标记:

$("#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
    },
});

In this case, all input fields are confirmed. But I want only the fields that are available to be added. How to do it?

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

2 个解决方案

#1


14  

Posted on behalf of OP:

代表OP发表:

I'm using the beforeTagAdded function I found my answer:

我正在使用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;
            }

        },

#2


1  

I was able to achieve this by adding the following check to the top of the createTag function:

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

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

And then adding this option in the tag-it call:

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

onlyAvailableTags: true

#1


14  

Posted on behalf of OP:

代表OP发表:

I'm using the beforeTagAdded function I found my answer:

我正在使用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;
            }

        },

#2


1  

I was able to achieve this by adding the following check to the top of the createTag function:

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

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

And then adding this option in the tag-it call:

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

onlyAvailableTags: true