如何获取自动完成中所选项目的索引?

时间:2012-12-12 12:31:13

标签: jquery jquery-ui

我正在使用jQueryUI的自动完成功能。它正确地列出了项目列表,我可以从列表中进行选择。我遇到的问题是如何从源事件中使用的数组中获取所选项的索引。

var options = {
    select: function() {
        // problem is here, I'm not able to see the correct index number of the selected item and always say -1
        alert($.inArray($("#searchAText").val()), arrayA); 
    },
    source: function(req, response) {
        var re = $.ui.autocomplete.escapeRegex(reg, term);
        var matcher = new RegExp("^" + re + "i");
        response($.grep(arrayA, function(item, index) {
            return matcher.test(item);
        })); 
    }
};
}

1 个答案:

答案 0 :(得分:2)

试试这个:

select: function(event, ui) {
     alert($.inArray(ui.item.value, arrayA));
}