【问题标题】:tag-it: function for select in autocomplete not workingtag-it:自动完成中的选择功能不起作用
【发布时间】:2015-03-25 10:44:20
【问题描述】:

我正在使用 tag-it 插件从用户那里获取标签输入。用于自动完成的标签源是通过一个 ajax 调用返回 json 对象,我使用这些对象映射标签的值和名称以显示给用户。 json 对象还包含我不想向用户显示但发送到服务器而不是标签标签/值的每个标签的 ID。为此,我想我可以使用自动完成中通常可用的“选择”选项下的功能。此函数将维护用户选择的所有 ID 的数组。但是当我选择一个标签时,选择函数并没有被调用。我使用的代码如下:

   $("#myTags").tagit({
        allowSpaces: true,
        autocomplete: {
            source: function (request, response) {
                $.ajax({
                    url: "http://localhost:5555/api/Tag",
                    dataType: "json",
                    data: {
                      strSearch: request.term
                    },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.Name, //Use rest of the data to map IDs
                                value: item.Name,
                                ID: item.ID
                            }
                        }));
                    }
                });
            },
            minLength: 1,
            select: function (event, ui) {
                console.log(ui.item.label + "=" + ui.item.ID);
            }

        }

    });

【问题讨论】:

    标签: jquery tag-it


    【解决方案1】:

    对我来说,使用 UP/DOWN 键和 ENTER 时选择不起作用。使用鼠标和标签突出显示。

    我尝试了其他帖子中建议的一些密钥检测代码(如果密钥 == 13),但没有一个对我有用。

    我的解决方案是将焦点事件放在我临时存储最后一个具有焦点的对象的位置。然后,当用户按回车键或用鼠标单击时,我检查键入的标签是否与最后一个焦点标签相同。这与标签被“选中”的效果相同:

    // Variable used to store temporarily the focused label from
    // the autocomplete list.
    var focusedTag   = null;
    
    $('#tags-list').tagit({
    
        fieldName: 'tags',
    
        autocomplete: {
    
            source: function (request, response) { /* Load your list... */ },
    
            // Stored the tag that received focus temporarily, to solve
            // enter key problem.
            focus: function(event, ui) {
                focusedTag = ui.item;
            },
    
            delay: 100,
            minLength: 3
        },
    
        afterTagAdded: function (event, ui) {
    
            if (focusedTag !== null &&
                    focusedTag.label === ui.tagLabel) {
                // Same as if it was selected...
            }
    
            focusedTag   = null;
    
        },
    
        allowSpaces: true
    });
    

    干杯。

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多