How to add class for li selected jquery ui autocomplete?

afrila gunadi

i am making form input with autocomplete multiple values and perfectly works, but i get some problem. i want if the first option I choose Alvin, then the second option I choose Yossy. so when I want to choose Yossy, option Alvin be there with a red background ( class active )

I tried to learn the method from jquery ui autocomplete, but I am still confused as to how it works.

this my code

$(document).ready(function() {
var availableTags = [
    "Alvin Lim",
    "Khia Wijaya",
    "Suryo Guritno",
    "Adjie Hidayat",
    "Yossy Mo",
    "Iwan Prasetyo",
];

function split( val ) {
    return val.split( /,\s*/ );
}

function extractLast( term ) {
    return split( term ).pop();
}

$( "#account" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {

    /* $('#keyword input#account').css('background','url(../image/arrow-up.png) no-repeat 95% center #bcbcbc'); */
    if ( event.keyCode === $.ui.keyCode.TAB &&
        $( this ).data( "ui-autocomplete" ).menu.active ) {

        event.preventDefault();
    }
})

 .autocomplete({
    minLength: 0,
    source: function( request, response ) {
    // delegate back to autocomplete, but extract the last term
    response( $.ui.autocomplete.filter(
    availableTags, extractLast( request.term ) ) );
    },

focus: function() {
    // prevent value inserted on focus

    return false;
},


select: function( event, ui ) {
    var terms = split( this.value );
    // remove the current input
    terms.pop();

    // add the selected item
    terms.push( ui.item.value );
    // add placeholder to get the comma-and-space at the end
    terms.push( "" );
    this.value = terms.join( ", " );
    return false;
}


});
});

anyone can help me? thanks in advance, regards :)

Outlooker

I have'nt checked it out mate,this is juz a work-around.There might be other correct ways of doing it.But i think something like this might help u mate :)

$(document).ready(function() {
        var terms;
    var availableTags = [
        "Alvin Lim",
        "Khia Wijaya",
        "Suryo Guritno",
        "Adjie Hidayat",
        "Yossy Mo",
        "Iwan Prasetyo",
    ];

    function split( val ) {
        return val.split( /,\s*/ );
    }

    function extractLast( term ) {
        return split( term ).pop();
    }

    $( "#account" )
    // don't navigate away from the field on tab when selecting an item
    .bind( "keydown", function( event ) {

        /* $('#keyword input#account').css('background','url(../image/arrow-up.png) no-repeat 95% center #bcbcbc'); */
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).data( "ui-autocomplete" ).menu.active ) {

            event.preventDefault();
        }
    })

     .autocomplete({
        minLength: 0,
        source: function( request, response ) {
        // delegate back to autocomplete, but extract the last term
        response( $.ui.autocomplete.filter(
        availableTags, extractLast( request.term ) ) );
        },

    focus: function() {
        // prevent value inserted on focus

        return false;
    },


    select: function( event, ui ) {
        terms = split( this.value );
        // remove the current input
        terms.pop();

        // add the selected item
        terms.push( ui.item.value );
        // add placeholder to get the comma-and-space at the end
        terms.push( "" );
        this.value = terms.join( ", " );
        return false;
    }


    }).data('autocomplete')._renderItem = function( ul, item ) {
        if($.inArray(item.label, terms) > -1 ){
         return $( "<li></li>" )
                .data( "item.autocomplete", item )
        .append( '<a class="selected">' + item.label + '</a>' )
                .appendTo( ul );
        }else{
                return $( "<li></li>" )
                .data( "item.autocomplete", item )
        .append( '<a>' + item.label + '</a>' )
                .appendTo( ul );
        }

        };
        //}
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to get focused to a selected li using navigation keys in jquery

来自分类Dev

JQuery UI Autocomplete and hidden field

来自分类Dev

jQuery选择器li:selected

来自分类Dev

选择后,jQuery UI AutoComplete插入<br>

来自分类Dev

如何使用 AutoComplete JQuery UI 触发“开启”

来自分类Dev

通过JQuery在父<li>中查找.class

来自分类Dev

包含li class =“ active”的jQuery open div

来自分类Dev

包含li class =“ active”的jQuery open div

来自分类Dev

页面上的jQuery-ui和js-autocomplete冲突?

来自分类Dev

向jQuery ui-autocomplete附加并保留其他元素

来自分类Dev

Jquery-UI -AutoComplete 不适用于 BootBox 模式

来自分类Dev

jQuery datatables add selected row to second table

来自分类Dev

jquery-ui gem:找不到导入文件或不可读:jquery-ui / autocomplete

来自分类Dev

从原始 <option> 向 jQuery UI selectmenu <li> 添加类

来自分类Dev

向jQuery UI的ui-autocomplete Combobox Div添加自定义类

来自分类Dev

向jQuery UI的ui-autocomplete Combobox Div添加自定义类

来自分类Dev

使用jquery显示<li>标记内的所有子<ui>和<li>

来自分类Dev

jQuery n子过滤器li.class

来自分类Dev

add data attribute to jquery ui datepicker on initialization

来自分类Dev

使用JQuery可以遍历ColumnChooser的multiselect ul.selected标记中的li吗?

来自分类Dev

How to get the selected date from jquery datepicker

来自分类Dev

如何将数据作为函数参数中的响应返回到对象(如jquery ui autocomplete)

来自分类Dev

如何将jQuery UI Autocomplete插件的Source属性设置为html表的内容

来自分类Dev

jQuery UI Autocomplete是否可以使用多个输入字段中的值

来自分类Dev

jQuery .val()不会在ui-autocomplete选择处理程序上进行任何更改

来自分类Dev

How to attach additional properties for the jQuery Autocomplete select event

来自分类Dev

jQuery .autocomplete和html

来自分类Dev

给jQuery AutoComplete添加延迟

来自分类Dev

如何使用jQuery Autocomplete

Related 相关文章

  1. 1

    How to get focused to a selected li using navigation keys in jquery

  2. 2

    JQuery UI Autocomplete and hidden field

  3. 3

    jQuery选择器li:selected

  4. 4

    选择后,jQuery UI AutoComplete插入<br>

  5. 5

    如何使用 AutoComplete JQuery UI 触发“开启”

  6. 6

    通过JQuery在父<li>中查找.class

  7. 7

    包含li class =“ active”的jQuery open div

  8. 8

    包含li class =“ active”的jQuery open div

  9. 9

    页面上的jQuery-ui和js-autocomplete冲突?

  10. 10

    向jQuery ui-autocomplete附加并保留其他元素

  11. 11

    Jquery-UI -AutoComplete 不适用于 BootBox 模式

  12. 12

    jQuery datatables add selected row to second table

  13. 13

    jquery-ui gem:找不到导入文件或不可读:jquery-ui / autocomplete

  14. 14

    从原始 <option> 向 jQuery UI selectmenu <li> 添加类

  15. 15

    向jQuery UI的ui-autocomplete Combobox Div添加自定义类

  16. 16

    向jQuery UI的ui-autocomplete Combobox Div添加自定义类

  17. 17

    使用jquery显示<li>标记内的所有子<ui>和<li>

  18. 18

    jQuery n子过滤器li.class

  19. 19

    add data attribute to jquery ui datepicker on initialization

  20. 20

    使用JQuery可以遍历ColumnChooser的multiselect ul.selected标记中的li吗?

  21. 21

    How to get the selected date from jquery datepicker

  22. 22

    如何将数据作为函数参数中的响应返回到对象(如jquery ui autocomplete)

  23. 23

    如何将jQuery UI Autocomplete插件的Source属性设置为html表的内容

  24. 24

    jQuery UI Autocomplete是否可以使用多个输入字段中的值

  25. 25

    jQuery .val()不会在ui-autocomplete选择处理程序上进行任何更改

  26. 26

    How to attach additional properties for the jQuery Autocomplete select event

  27. 27

    jQuery .autocomplete和html

  28. 28

    给jQuery AutoComplete添加延迟

  29. 29

    如何使用jQuery Autocomplete

热门标签

归档