【问题标题】:Textext.js - Remove all tags and clean textareaTextext.js - 删除所有标签并清理 textarea
【发布时间】:2018-12-10 16:28:56
【问题描述】:

我有一个问题,我正在使用Textext.js 列出数据库中的用户,我正在使用带有自定义数据对象的标签({name: 'nameTag', id: 1})问题是我需要用“cleanFields”按钮清理文本字段和标签,我找不到办法。这是我的代码:

$("#myTextarea").textext({
    plugins: 'tags prompt autocomplete',
    prompt: 'Add one...',
    ext: {
        itemManager: {
            items: [],
            stringToItem: function (str) {
                for (var i = 0; i < this.items.length; i++)
                    if (this.items[i].name == str) {
                        id = this.items[i].id;
                        break;
                    }
                return { name: str, id: id }; //tags with custom data objects
            },
            itemToString: function (item) {
                this.items.push(item);
                return item.name;

            },
            compareItems: function (item1, item2) {
                return item1.name == item2.name;
            }
        }
    }
})
.bind('getSuggestions', function (e, data) {
    var list = f_GetListTags() //this returns the list from the database
    , textext = $(e.target).textext()[0],
        query = (data ? data.query : '') || '';
    $(this).trigger(
        'setSuggestions',
        { result: textext.itemManager().filter(list, query) }
    );
})

按钮:

$("#cleanFields").click(function () {
    $('#myTextarea').val(''); //this cleans the text area

    //but i also need to remove all tags :(
});

请帮帮我! :(

【问题讨论】:

    标签: javascript jquery jquery-textext


    【解决方案1】:

    标签存储在 div 类 .text-tags 中,并且它们有一个锚 a.text-remove 可以用来删除它们 - 你会的如果您使用它,则不需要清除 textarea,因为它已经是删除的一部分:

    $("#cleanFields").click(function () {
        // This will clear the text area and remove the tags :)
        $(".text-tags a.text-remove").trigger("click");
    });
    

    警告:这将清除所有带有标签的文本区域。除非您只使用 1 个标签输入字段,否则您应该考虑使您的选择器(ids / classes)更加具体以避免冲突。

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2019-01-08
      • 1970-01-01
      • 2011-11-24
      相关资源
      最近更新 更多