简体   繁体   中英

How to make dependant multiple selectable autocomplete using jQuery ui autocomplete

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.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;
            }
        });
});

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",
    ]
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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