开发者

How to make dependant multiple selectable autocomplete using jQuery ui autocomplete

开发者 https://www.devze.com 2023-03-02 23:40 出处:网络
I am using jquery-ui\'s autocomplete in my project. It works great. Now I want to add autocomplete that is dependant on a previous select option.

I am using jquery-ui's autocomplete in my project. It works great. Now I want to add autocomplete that is dependant on a previous select option.

I have one select and a text input with autocomplete.

 <select id='type'>
     <option value='languages'>Languages</option>
     <option value='OS'>Operating Systems</option>
 </select>

 <input type='text' id='tags' />

I use a similar function for the autocomplete and it works.

    $(function() {
    var languages = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "Scheme"
    ];
    var os = [
        "Windows",
        "Mac OS X",
        "Chrome",
    ];
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    $( "#tags" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "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(
                    languages, extractLast( request.ter开发者_StackOverflowm ) ) );
            },
            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;
            }
        });
});

How can I link it to my onchange event of the previous select. So when I select operating systems. The autocomplete changes the array from languages to os.

I appreciate any help.


Try something along the following. You'll also need to restructure how your suggestion array is formatted slightly so it's more workable!

$("#type").change(function() {
    var source = $(this).val().toLowerCase();

    $("#tags").source(function( request, response ) {
                // delegate back to autocomplete, but extract the last term
                response( $.ui.autocomplete.filter(
                    sources[source], extractLast( request.term ) ) );
    });
});

var sources = {
    "languages": [
        "ActionScript",
        "AppleScript",
        "Asp",
        "Scheme"
    ],

    "os": [
        "Windows",
        "Mac OS X",
        "Chrome",
    ]
};
0

精彩评论

暂无评论...
验证码 换一张
取 消