【问题标题】:Jquery tokenInput - Cannot read property of undefined problemJquery tokenInput - 无法读取未定义问题的属性
【发布时间】:2019-05-02 08:13:25
【问题描述】:

我在做什么:

  • 执行 HTTP 获取请求
  • 当我得到响应时,我在 tokenInput 中添加了一个属性。 请看下面的代码

    $http.get('/product/getDetails/'+$scope.productId)
    .then(function (response) {
    
        var brand = response.brand;
        var manufacturer = response.manufacturer;
    
        $http.get('/category/getAllTypes')
        .then(function (response) {
    
        }
    
    
        $timeout(function () {
                $(document).ready(function() {
                    $('#brand').tokenInput("add", {id: brand._id, name: brand.name, isActive: 0, checkStatus: 1});
                    $('#manufacturer').tokenInput("add", {id: manufacturer._id, name: manufacturer.name, isActive: 0, checkStatus: 1});
            })
        }, 500);
    }
    

问题:

我收到以下错误。

TypeError: Cannot read property 'add' of undefined
at a.fn.init.add (jquery.tokeninput.js:110)
at a.fn.init.$.fn.tokenInput (jquery.tokeninput.js:126) 

我尝试了什么:

我尝试通过

初始化 tokenInput
    $('#brand').tokenInput("../plugins/tokeninput/js/jquery.tokeninput.js");
    $('#brand').tokenInput("add", {id: brand._id, name: brand.name, isActive: 0, checkStatus: 1});

但它显示了两个 tokenInputs。

我搜索了很多,但没有找到解决方案。任何帮助/建议将不胜感激。

【问题讨论】:

  • 您是否尝试在初始化函数以从 url 获取结果后调用它?

标签: jquery node.js jquery-tokeninput


【解决方案1】:

您是否尝试在初始化函数以从 url 获取结果后调用它?

例如

$('#brand').tokenInput(/yourApi/, {  

// define the functions:  

    onAdd : function (item) {  
                var tokens = $('#brand').tokenInput('get');
                ...
              },

    ....

}

// now, after above function, you try to populate it "programmatically"

$('#brand').tokenInput("add", {id: brand._id, name: brand.name, isActive: 0, checkStatus: 1});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2021-10-31
    • 2020-05-09
    • 2020-07-24
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多