jQuery UI Autocomplete with hybrid text/id search

后端 未结 4 591
野性不改
野性不改 2020-12-14 04:31

I\'m having a lot of trouble making jQuery\'s autocomplete widget work for me. I am using a list of key/value pairs from a server.

I have the following requirements

4条回答
  •  我在风中等你
    2020-12-14 04:59

    When i presume you have the following fields set:

    You need to init following js (it works with jQuery 1.5.2):

    $(document).ready(function() {
        $('.autocomplete-reference').each(function () {
            $(this).keydown(function(e){
                /*
                * this will reset hidden id reference on each visible field manual change
                * we have to bind it on keydown because we want to recognize event of submiting form by enter after autocomplete set
                */
                if (e.keyCode != 13) {
                    $(this).siblings('input[type=hidden]').each(function () {
                        $(this).val('');
                    });
                }
            });
            $(this).autocomplete({
                minLength: 1,
                /* you can set appending of autocomplete menu into some container to set css contextually
                appendTo: '#someElem',*/
                source: sourceFunction,
                search:function (event, ui) {
                    //TODO ...spinner init...
                },
                open:function (event, ui) {
                    /* you can grab autocomplete menu widget by this to manipulate some recognizing id, etc..
                     * $(this).autocomplete('widget')
                    */
                    //TODO ..stop spinner ...
                    $(this).keydown(function(e){
                        /* this is important for live reference updates while arrow keys changes  */
                        if (e.keyCode == 37 || e.keyCode == 39) {
                           $($(this).data('autocomplete').menu.active).find('a').trigger('click');
                        }
                    });
                },
                focus: function(event, ui) {
                    /* here we'll map our data object onto both fields */
                    $(this).val(ui.item.label);
                    $(this).siblings('input[type=hidden]').each(function () {
                        $(this).val(ui.item.key);
                    });
                },
                select: function(event, ui) {
                    /* here we'll map our data object onto both fields */
                    $(this).val(ui.item.label);
                    $(this).siblings('input[type=hidden]').each(function () {
                        $(this).val(ui.item.key);
                    });
                },
                close: function(event, ui) {
                    //TODO ..stop spinner ...
                }
            });
        });
    });
    

    It's very nice to have some serverside validation which can convert exact :visible field values into references for quick typing folks of course.

提交回复
热议问题