简体   繁体   English

在typeahead.js中触发“选定”事件

[英]Triggering “selected” event in typeahead.js

I am using typeahead.js vs. 0.9.3 and populating the dataset using local . 我正在使用typeahead.js与0.9.3,并使用local填充dataset

I am binding functions to the typeahead:selected and typeahead:autocompleted events so that I can populate some other hidden input fields in the form using information in the selected typeahead datum. 我将函数绑定到typeahead:selectedtypeahead:autocompleted事件,以便可以使用选定的typeahead数据中的信息填充表单中的其他一些隐藏输入字段。

<form id="customer-form" action="/customer-form" method="post">
    <input type="text" name="customer-typeahead" placeholder="Customer name" id="customer-typeahead"/>

    <input type="hidden" name="customer-id" id="customer-id"/>
    <input type="hidden" name="customer-name" id="customer-name"/>   
</form>

I also have some HTML boxes on the page and what I would like to happen is for the information in those boxes to populate the typeahead as well as the hidden input fields. 我在页面上还有一些HTML框,我希望这些框中的信息可以填充预输入以及隐藏的输入字段。 In other words, for the html below, if a user clicks on a .copy-data div , the #customer-typeahead and #customer-name should be populated with the text in the clicked upon .customer-name div and #customer-id should be populated with the text in the clicked upon .customer-id div . 换句话说,对于以下html,如果用户单击.copy-data div.copy-data#customer-typeahead#customer-name中填充单击的.customer-name div#customer-id应该用单击的.customer-id div的文本填充。

<div class="copy-data">
    <div class="buffer-div">
        <div class="customer-id">1</div>
        <div class="customer-name">Andrew</div>
    </div>
</div>
<div class="copy-data">
    <div class="buffer-div">
        <p class="customer-id">2</p>
        <p class="customer-name">Bryan</p>
    </div>
</div>
<div class="copy-data">
    <div class="buffer-div">
        <div class="customer-id">3</div>
        <div class="customer-name">Cathy</div>
    </div>
</div>

I have the majority of this working with the following jquery: 我主要使用以下jquery进行此操作:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="../js/typeahead.min.js" type="text/javascript"></script>
<script src="../js/hogan.js" type="text/javascript"></script>
<script>

$(document).ready(function() {

    var typeahead_template = [
            '<div style="width: 190px; float:left;">{{value}}</div>',
            '<div style="float:right;">{{id}}</div>'
            ].join('')

    function changeTypeahead(obj, datum) {
            $('input#customer-id').val(datum.id);
            $('input#customer-name').val(datum.value);
    };

    $('#customer-typeahead').typeahead({
        local: [{"value": "Andrew", "id": "1"}, {"value": "Bryan", "id": "2"}, {"value": "Cathy", "id": "3"} ],
        limit: 5,
        template: typeahead_template,
        engine: Hogan
    }).bind('typeahead:selected', function(obj, datum) {
        changeTypeahead(obj, datum);
    }).bind('typeahead:autocompleted', function(obj, datum) {
        changeTypeahead(obj, datum);
    });

    $(".copy-data").click(function(){
        id = $(this).find(".customer-id").text();
        name = $(this).find(".customer-name").text();

        $("#customer-typeahead").typeahead('setQuery', name)
        $("#customer-typeahead").trigger('selected');
    });
});

</script>

When the user clicks on the .copy-data div , the appropriate customer name populates the input text box but not the hidden inputs. 当用户单击.copy-data div ,将在输入文本框中填充相应的customer name ,但不会填充隐藏的输入。 I was intending to somehow trigger the typeahead:selected event, which would pass the appropriate datum to the changeTypeahead function but that doesn't seem to be happening. 我打算以某种方式触发typeahead:selected事件,该事件会将适当的datum传递给changeTypeahead函数,但这似乎没有发生。

Is there a way to leverage the native functionality of the typeahead and its dataset this way or must I set the hidden inputs directly? 是否可以通过这种方式利用预输入及其数据集的本机功能,还是必须直接设置隐藏的输入?

UPDATE: To clarify, I was imagining that using setQuery would cause typeahead to "fire" on its own, ie match the query, determine the appropriate datum from the dataset on its own, and trigger all appropriate events. 更新:为澄清setQuery ,我在想使用setQuery会导致setQuery “触发”,即匹配查询,自行从数据集中确定适当的数据,并触发所有适当的事件。 I'd prefer to not have to recreate the entire datum object external from the typeahead dataset if it can be avoided 如果可以避免,我宁愿不必在typeahead数据集外部重新创建整个基准对象

You seem to be forgetting to pass the datum to your selected trigger as the second parameter. 您似乎忘了将datum作为第二个参数传递给selected触发器。 Try something like: 尝试类似:

$("#customer-typeahead").trigger('selected', {"id": id, "value": value});

See the documentation for jQuery.trigger 请参阅jQuery.trigger的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM