JQuery - 自动完成上的选择功能没有被调用?

时间:2013-03-11 18:06:41

标签: jquery autocomplete

我有以下代码将自动完成添加到我的textBox,自动完成显示并且一切正常,但是当用户选择一个项目时,选择回调不会被调用? 任何解决方案?

$(function() {

$("#txtItem").autocomplete({
    minLength: 1,
    source: function (request, response) {
        $.ajax({
            url: "/Correct url here",
            dataType: "json",
            data: {
                term: request.term
            },
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name
                    }
                }))
            },
            select: function (event, ui) {
                alert("sdfsdf");
                //$("#txtItemId").val(ui.item.value.ItemId);
            }
        })
    }
})

}

2 个答案:

答案 0 :(得分:2)

将代码重写为:

$(function() {

$("#txtItem").autocomplete({
    minLength: 1,
    source: function (request, response) {
        $.ajax({
            url: "/Correct url here",
            dataType: "json",
            data: {
                term: request.term
            },
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name
                    }
                }))
            }            
        })
    },
    select: function (event, ui) {
        alert("sdfsdf");
        //$("#txtItemId").val(ui.item.value.ItemId);
    }
})

}

让我知道它是否有效

答案 1 :(得分:2)

$(function() {

$("#txtItem").autocomplete({
minLength: 1,
source: function (request, response) {
    $.ajax({
        url: "/Correct url here",
        dataType: "json",
        data: {
            term: request.term
        },
        success: function (data) {
            response($.map(data, function (item) {
                return {
                    label: item.Name,
                    ItemId: item.ItemId//this is will put item id in memory
                }
            }))
        }            
    })
},//this is after the source property select is an auto complete property many examples screw this up and it is in side the ajax block of code
select: function (event, ui) {
    alert("sdfsdf");
    //$("#txtItemId").val(ui.item.value.ItemId);
  }
  })}

这是一个工作示例http://plnkr.co/edit/RbuRDh1xRIwyO1QamvCe?p=preview