Jquery autocomplete使用URL方式出错

$("#CustomerName").autocomplete({             
            //source: "/Test/Project/GetCompanySelectJson",
            source: [{ "label": "江苏灯为有限公司", "value": 2 }, { "label": "城中投资发展有限公司", "value": 4 }, { "label": "国啦为要", "value": 5 }],
            select: function (event, ui) {               
                $("#CustomerName").val(ui.item.label);
                $("#CustomerId").val(ui.item.value);                
                event.preventDefault();
            }
        });

使用上面的Source,autocomplete会索引出全部记录,而不是按输入的关键字,通过F12查看获得的数据,就是下面的Source值,换成下面的Source是正常使用,也就是说代码应该是没有问题的,为什么上面的Source就索引全部呢?


弑天下
浏览 571回答 1
1回答

MM们

表示不想研究autocomplete源码,不过我可以给出你另外的解决办法既然固定的source是正常的,那么可以尝试自己ajax获取source然后初始autocomplete$.ajax({type:'GET',url:'your url', success:function(res){    var source = JSON.parse(res)    $("#CustomerName").autocomplete({ source:source })}} 另外一方面,autocomplete的url方式的source应该是需要你服务器去做筛选,你可以查看请求是否附带了关键字
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript