jQuery UI autocomplete with item and id

前端 未结 11 1798
隐瞒了意图╮
隐瞒了意图╮ 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:39

    Assuming the objects in your source array have an id property...

    var $local_source = [
        { id: 1, value: "c++" },
        { id: 2, value: "java" },
        { id: 3, value: "php" },
        { id: 4, value: "coldfusion" },
        { id: 5, value: "javascript" },
        { id: 6, value: "asp" },
        { id: 7, value: "ruby" }];
    

    Getting hold of the current instance and inspecting its selectedItem property will allow you to retrieve the properties of the currently selceted item. In this case alerting the id of the selected item.

    $('#button').click(function() {
        alert($("#txtAllowSearch").autocomplete("instance").selectedItem.id;
    });
    

提交回复
热议问题