【问题标题】:TypeError:f[c] when adding a tag to bootstrap tags input elementTypeError:f[c] 将标签添加到引导标签输入元素时
【发布时间】:2014-11-11 06:58:39
【问题描述】:

我有 2 个输入元素,其中一个我写了一些字母,自动完成功能从 db 中获取数据并获取结果,一旦我选择了一个结果,我想将其作为标签添加到另一个输入元素中。

$('@ID("#input_to")').autocomplete({
        source: function (request, response) {
            jQuery.post('@Html.Raw(Url.Action("LoadAutocomplete", "Correspondence", new
            {
                                                                                        Area = "Correspondence"
        }))',
                request, response);
        },
        minLength: 3,
        select: function (event, ui) {
            try {
                var elt = $('@ID("#input_tags_test")').tagsinput('input');

                elt.tagsinput({
                    itemValue: 'id',
                    itemText: 'text'
                });

                elt.tagsinput('add', { id: ui.item.id, text: ui.item.value });

                console.log(elt.tagsinput('items'));
            } catch (err) {
                console.log(err);
            }
            jQuery('@ID("#input_to")').val(ui.item.id);
        }
    });

这里是html代码

<input type="text" id="@ID("input_tags_test")" class="form-control bootstrap-tagsinput" data-role="tagsinput">               
            <input type="text" id="@ID("input_to")" class="form-control" name="recipientsName" placeholder="Search for someone...">

它只在第一次工作.. 第二次抛出异常

TypeError: f[c] 不是函数 ...lement).remove();b.itemsArray.length>0;)b.itemsArray.pop();b.pushVal(),b.options...

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap


    【解决方案1】:

    我发现了如何修复它,我不应该定义 tagsinput('input') 两次 .. 我是 javascript 和 Web 开发的新手,我认为在 C# 中数据成员(在这种情况下为 elt)将是destroyed 和析构函数将在函数执行完毕后被调用,但显然这里不是这种情况,所以添加一个简单的 null 检查 if 语句解决了问题。

     if (elt == null) {
                        elt = $('@ID("#input_tags_test")').tagsinput('input');
    
                        elt.tagsinput({
                            itemValue: 'id',
                            itemText: 'text'
                        });
                    }
    

    然后从此幸福快乐;)

    this question helped me figure it out

    【讨论】:

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