jQuery自动完成必须匹配没有任何影响

时间:2011-03-14 22:17:33

标签: javascript jquery autocomplete jquery-ui-autocomplete

我正在使用jQuery 1.4.4和jQuery UI 1.8.9。我有一个非常好的自动填充字段,但是我想将输入仅限于自动完成后端提供的内容。

该文件说明:

  

mustMatch Boolean默认值:false

     

如果设置为true,则自动完成程序将为   仅允许显示的结果   在后端。注意非法   值导致空输入框。

但是,当我将mustMatch设置为true时,它没有任何区别 - 我仍然可以键入任何我喜欢的内容,但没有任何反应。

我很确定这是我正在做的事情,而不是一个错误,因为我没有在谷歌上看到任何指向那个的东西。

以下是代码段:

    $( ".client" ).autocomplete({
    minLength: 2,
    mustMatch: true,
    source: function( request, response ) {
        var term = request.term;
        if ( term in client_cache ) {
            response( client_cache[ term ] );
            return;
        }

        client_lastXhr = $.getJSON( "amex/start.php?action=autocomplete&field=client", request, function( data, status, xhr ) {
            client_cache[ term ] = data;
            if ( xhr === client_lastXhr ) {
                response( data );
            }
        });
    }
});

谁能看到我做错了什么?谢谢你的帮助!

3 个答案:

答案 0 :(得分:6)

您可能会在documentation page上为已弃用的插件找到此选项。 jQueryUI的当前文档位于here

看起来他们删除了mustMatch选项,但根据this StackOverflow question,您可以自行实施。

答案 1 :(得分:0)

对于那些需要完整且更简单解决方案的人来说,试试这个......

$("#my_autocomplete").autocomplete({
    source: "my_data.php",
    change: function(e, ui) {
        if (!ui.item) {
            $(this).val("");
        }
    },
    response: function(e, ui) {
        if (ui.content.length == 0) {
            $(this).val("");
        }
    }
}).on("keydown", function(e) {
    if (e.keyCode == 27) {
        $(this).val("");
    }
});

JSFiddle示例:http://jsfiddle.net/dfc83/

答案 2 :(得分:0)

如果你想清除,只是为了在那里的木头里面返回一个值判断json数据,你可以将它清空。

如果想要清空,只需要判断返回的JSON里面的某值有木有数据,然后清空就可以啦。

function selectRegion(){
$("#saleRegionSelect").autocomplete({
    source: function (request, response) {
        var cityId=$("[name='city']").val();
        var oppType=$("[name='primaryBiz']:checked").val();
        var site= $.trim($("#channel").val());
        if(oppType==undefined){
            return false;
        }
        if(site==undefined){
            return false;
        }
        $.ajax({
            url: rootPath + "ajax/regionByCityAndSite",
            dataType: "json",
            type: "post",
            data: {
                region: request.term,
                city:cityId,
                site:site,
                oppType:oppType
            },
            success: function (data) {
                if (!data.rows) { //Look at this line

                    $('#saleRegionSelect').val('');//clear
                    $('#saleRegion').val(''); //clear

                    response(($.map([], function (item) {
                        return {}
                    })));
                } else {
                    response(($.map(data.rows, function (item) {
                        return {
                            label: item.region + ($.trim(item.saleName)==""?"":' (' + item.saleName + ')'),
                            value: item.region + ($.trim(item.saleName)==""?"":' (' + item.saleName + ')'),
                            id: item.id
                        }
                    })));
                }
            }
        })
    },
    minLength: 1, 
    select: function (event, ui) {
        $("#saleRegion").val(ui.item.id);
    }
});

}

=>杰森:

{
"status": 200,
"message": "查询数据成功!",
"rows": [{
    "id": "678140138089353216",
    "oppType": 10,
    "saleId": "201402201431456839e855",
    "saleName": "陈",
    "creator": "zhangdanling",
    "createTime": "2015-12-19 17:26:29",
    "modifier": "zhangdanling",
    "modifyTime": "2015-12-22 11:11:35",
    "cityId": 147,
    "site": 1,
    "region": "测试测试",
    "remark": ""
}, {
    "id": "678847511099609088",
    "oppType": 10,
    "saleId": "201402201431456839e855",
    "saleName": "陈",
    "creator": "zhangdanling",
    "createTime": "2015-12-21 16:17:23",
    "modifier": "zhangdanling",
    "modifyTime": "2015-12-22 15:55:59",
    "cityId": 147,
    "site": 1,
    "region": "测试一区",
    "remark": ""
}, {
    "id": "678910954649423872",
    "oppType": 10,
    "saleId": "201402201431456839e855",
    "saleName": "陈",
    "creator": "zhangdanling",
    "createTime": "2015-12-21 20:29:30",
    "modifier": "zhangdanling",
    "modifyTime": "2015-12-21 20:29:30",
    "cityId": 147,
    "site": 1,
    "region": "测试区域三",
    "remark": ""
}, {
    "id": "679596181579374592",
    "oppType": 10,
    "saleId": "201407081036237ccfe9dc",
    "saleName": "张",
    "creator": "zhangdanling",
    "createTime": "2015-12-23 17:49:05",
    "modifier": "zhangdanling",
    "modifyTime": "2015-12-23 17:49:14",
    "cityId": 147,
    "site": 1,
    "region": "测试一下",
    "remark": ""
}, {
    "id": "680322348053700608",
    "oppType": 10,
    "saleId": "201407081036237ccfe9dc",
    "saleName": "张",
    "creator": "SYS_INIT",
    "createTime": "2015-12-25 17:54:20",
    "modifier": "SYS_INIT",
    "modifyTime": "2015-12-25 17:54:20",
    "cityId": 147,
    "site": 1,
    "region": "测试一下001",
    "remark": ""
}, {
    "id": "681308156487278592",
    "oppType": 10,
    "saleId": "201402201431456839e855",
    "saleName": "陈",
    "creator": "zhangdanling",
    "createTime": "2015-12-28 11:11:56",
    "modifier": "zhangdanling",
    "modifyTime": "2015-12-28 11:11:56",
    "cityId": 147,
    "site": 1,
    "region": "测试离职销售区域",
    "remark": ""
}]

}