jQuery UI autocomplete with item and id

前端 未结 11 1817
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 00:32

I have the following script which works with a 1 dimensional array. Is it possible to get this to work with a 2 dimensional array? Then whichever item is selected, by clic

11条回答
  •  星月不相逢
    2020-11-29 00:50

    You need to use the ui.item.label (the text) and ui.item.value (the id) properties

    $('#selector').autocomplete({
        source: url,
        select: function (event, ui) {
            $("#txtAllowSearch").val(ui.item.label); // display the selected text
            $("#txtAllowSearchID").val(ui.item.value); // save selected id to hidden input
        }
    });
    
    $('#button').click(function() {
        alert($("#txtAllowSearchID").val()); // get the id from the hidden input
    }); 
    

    [Edit] You also asked how to create the multi-dimensional array...

    You should be able create the array like so:

    var $local_source = [[0,"c++"], [1,"java"], [2,"php"], [3,"coldfusion"], 
                         [4,"javascript"], [5,"asp"], [6,"ruby"]];
    

    Read more about how to work with multi-dimensional arrays here: http://www.javascriptkit.com/javatutors/literal-notation2.shtml

提交回复
热议问题