$("#myheadShops").autocomplete({
        source: function (request, response) {
            var value = $("#agents").val();
            var value1 = $("#myheadShops").val();
            var url = "../Vip/GetHeadShops";
            $.ajax({
                url: url,//"/Sys/Vip/GetHeadShops",
                dataType: "json",
                data: { id: value, value: value1, featureClass: "P", style: "full", maxRows: 12, name_startsWith: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.Id + "-" + item.Name, value: item.Name }
                    }));
                }
            });
        },
        select: function (event, ui) {
            setValue(ui.item ? ui.item.label : "", "headShops");
        }
    });这是我的JS。。,现在有一个问题。就是当我在文本框输入的文字只有一个匹配项时,自动默认选上,也就是不需要我手动点下那个选项了就自动选上。请问这个该怎么做。。求大侠帮忙,解答。。号上只有这么多分了。希望大侠们帮下忙。

解决方案 »

  1.   

        $("#myheadShops").autocomplete({
            source: function (request, response) {
                var value = $("#agents").val();
                var value1 = $("#myheadShops").val();
                var url = "../Vip/GetHeadShops";
                $.ajax({
                    url: url, //"/Sys/Vip/GetHeadShops",
                    dataType: "json",
                    data: { id: value, value: value1, featureClass: "P", style: "full", maxRows: 12, name_startsWith: request.term },
                    success: function (data) {
                        if (data.length == 1) { //只有一条,不知道你json结构,根据下面的代码猜测的,自己修改
                            setValue(ui.item ? data[0].Id + "-" + data[0].Name : "", "headShops");
                        }
                        else response($.map(data, function (item) {
                            return { label: item.Id + "-" + item.Name, value: item.Name }
                        }));
                    }
                });
            },
            select: function (event, ui) {
                setValue(ui.item ? ui.item.label : "", "headShops");
            }
        });