Twitter Bootstrap typeahead:使用`this`获取上下文/调用元素

Pet*_*cco 18 javascript jquery closures typeahead twitter-bootstrap

我正在使用Twitter Bootstrap 2.1.1和jQuery 1.8.1 的Typeahead组件

我试图从typeahead的updater函数中访问文本框元素.这是我目前的代码,效果很好:

$('#client-search').typeahead({
    source: function (query, process) {
        $.get(url, { query: query }, function (data) {
            labels = [];
            mapped = {};
            $.each(data, function(i,item) {
                mapped[item.label] = item.value;
                labels.push(item.label);
            });
            process(labels);
        });
    }
    ,updater: function (item) {
        $('#client-id').val(mapped[item]);
        return item;
    }
    ,items: 10
    ,minLength: 2
});
Run Code Online (Sandbox Code Playgroud)

我在同一页面上有很多预先搜索框.每个搜索框都有一个id #xxx-search和一个带id的相应隐藏输入#xxx-id.我想通过这样做来重复使用相同的代码:

$('#client-search, #endClient-search, #other-search').typeahead({

    ...

    ,updater: function (item) {
        var target = $(this).attr('id').split('-')[0] + '-id';
        $('#'+target).val(mapped[item]);
        return item;
    }

    ...
Run Code Online (Sandbox Code Playgroud)

我认为这this将引用正在使用的文本框元素,但显然不是,因为我在firebug中得到一个未定义的错误:

$(this).attr("id") is undefined
Run Code Online (Sandbox Code Playgroud)

当我使用this而不是$(this),我得到:

this.attr is not a function
Run Code Online (Sandbox Code Playgroud)

谁能帮忙做这个工作?


更新:答案,还有更多!

感谢benedict_w的回答,不仅现在这种方法完美无缺,而且在可重用性方面我也做得更好.

这是我<input>的样子:

<input type="text" autocomplete="off"
    id="client-search"
    data-typeahead-url="/path/to/json"
    data-typeahead-target="client-id">
<input type="hidden" id="client-id" name="client-id">
Run Code Online (Sandbox Code Playgroud)

请注意搜索框如何引用隐藏的ID.这是新的js:

$(':input[data-typeahead-url]').each(function(){
    var $this = $(this);
    $this.typeahead({
        source: function (query, process) {
            $.get($this.attr('data-typeahead-url'), { query: query }, function (data) {
                labels = [];
                mapped = {};
                $.each(data, function(i,item) {
                    mapped[item.label] = item.value;
                    labels.push(item.label);
                });
                process(labels);
            });
        }
        ,updater: function (item) {
            $('#'+$this.attr('data-typeahead-target')).val(mapped[item]);
            return item;
        }
        ,items: 10
        ,minLength: 2
    });
});
Run Code Online (Sandbox Code Playgroud)

ben*_*t_w 24

您在另一个函数内部,因此您需要保存对外部闭包中元素的引用,然后可以将其传递到内部闭包的事件处理程序中.您可以使用$.each()调用来循环选择器,每次都保存对元素(this)的引用 - 例如

$('#client-search, #endClient-search, #other-search').each(function() {
    var $this = $(this);
    $this.typeahead({
        ...

        ,updater: function (item) {
            var target = $this.attr('id').split('-')[0] + '-id';
            $('#'+target).val(mapped[item]);
            return item;
        }
        ...
     });
Run Code Online (Sandbox Code Playgroud)


小智 6

接受的答案是一个很好的答案,但只是想分享一个实际上不需要外部封闭的替代方案.我正在使用bootstrap v2.3.1,您可以从中获得输入this.$element.

例如:

$(':input[data-typeahead-url]').typeahead({
    source: function (query, process) {
        $.get(this.$element.attr('data-typeahead-url'), { query: query }, function (data) {
            labels = [];
            mapped = {};
            $.each(data, function(i,item) {
                mapped[item.label] = item.value;
                labels.push(item.label);
            });
            process(labels);
        });
    }
    ,updater: function (item) {
        $('#'+this.$element.attr('data-typeahead-target')).val(mapped[item]);
        return item;
    }
    ,items: 10
    ,minLength: 2
});
Run Code Online (Sandbox Code Playgroud)