简体   繁体   English

当我在 typeahead 中选择一个元素时如何创建一个 div?

[英]How do i create a div when i select an element in typeahead?

Here is my function:这是我的功能:

$('input.typeahead').bind('typeahead:selected', function (obj, datum, name){
     alert(JSON.stringify(obj)); // object
     // outputs, e.g., {"type":"typeahead:selected","timeStamp":1371822938628,"jQuery19105037956037711017":true,"isTrigger":true,"namespace":"","namespace_re":null,"target":{"jQuery19105037956037711017":46},"delegateTarget":{"jQuery19105037956037711017":46},"currentTarget":
     alert(JSON.stringify(datum)); // contains datum value, tokens and custom fields
     // outputs, e.g., {"redirect_url":"http://localhost/test/topic/test_topic","image_url":"http://localhost/test/upload/images/t_FWnYhhqd.jpg","description":"A test description","value":"A test value","tokens":["A","test","value"]}
     // in this case I created custom fields called 'redirect_url', 'image_url', 'description'
     alert(JSON.stringify(name)); // contains dataset name
     // outputs, e.g., "my_dataset"
});

I want to be able to create a div when I click on a suggestion, but I can't figure that out.我希望能够在单击建议时创建一个 div,但我无法弄清楚。 How can I do this?我怎样才能做到这一点? I tried to append an onclick event but it isn't even called.我试图附加一个 onclick 事件,但它甚至没有被调用。 Any help is highly appreciated :)任何帮助都非常感谢:)

You can create an div element and inject it after the input using the following syntax:您可以使用以下语法创建一个div元素并在输入后注入它:

$('input.typeahead').bind('typeahead:selected', function(obj, datum, name) {

    var $div = $('<div/>', {
        class: 'whatever'
    });

    $div.append('some text');

    //insert after input
    $(this).after($div);

    // or you can inject it into a specific element
    $('#elementId').append($div);
});

暂无
暂无

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

相关问题 如何在div元素上调用方法时创建具有动态使用的id的div元素? - How do I create a div element with an id that is dynamically used when calling a method on the div element? 将 select 元素添加到 div 时,如何获取 asp-items 来填充 select? - How do I get asp-items to populate a select when adding select element to a div? 如何使用jQuery选择div内的元素 - How do I select an element inside of a div using Jquery 我如何 select 使用 jquery 的 div 中的元素 - how do i select a element within a div using jquery 如何动态添加具有自己独特的selectahead-on-select事件的AngularJS typeahead组件? - How do I dynamically add a AngularJS typeahead component that has its own unique typeahead-on-select event? 根据分类名称过滤div时,如何选择最初显示的div元素? - How do I select which div element to show initially when I am filtering divs based on their class name? 在内部进行特定选择时,如何创建文本字段 <select>元件? - How do I create a textfield when a certain selection is made inside a <select> element? 如果知道元素ID,如何在contenteditable div中选择(获取范围或选择对象)元素? - How do I select (get range or selection object) an element in a contenteditable div if I know the element ID? 如果选择了 select 标记中的选项,我如何创建元素 - How do I create an element if the option in select tag is selected 当我单击同一div或在div之外时,如何隐藏和切换popover元素? - How do i hide and toggle popover element when i click on the same div or outside of the div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM