首页 > 解决方案 > 无法设置未定义的属性“url”

问题描述

在下面的示例中,我创建了typeahead该类的一个实例

    var ta = $.typeahead({
        input: '.super',
        minLength: 0,
        maxItem: 15,
        order: "asc",
        hint: true,
        href: "",
        searchOnFocus: true,
        display: ["name"],
        template: "{{name}}",
        source: {
            "SearchResults": {
                //ajax: {
                //    url: "/search/" + id,
                //    path: ""
                //}
            }
        },
        callback: {
            onClickAfter: function (node, a, item, event) {
                event.preventDefault;
                debugger;
            }
        },
        debug: true
    });

稍后,通过使用id通过以下方法获得的变量

    $("input").focus(function () { 
        id = $(this).attr("id");
        ta.source.ajax.url = "/search/" + id;
    }); 

我得到了,Cannot set property 'url' of undefined因为ta.source.ajax实际上是空的。我该如何解决?

简而言之,我不想为表单的每个输入创建重复的代码

标签: jquerytypeahead.js

解决方案


通过Typeahead 实例的属性配置SearchResults组。options

sourceTypeahead 实例的属性是从options.

ta.options.source.SearchResults.ajax.url = "/search/" + id;

推荐阅读