1. 程式人生 > >jquery autocomplete ajax動態獲取資料

jquery autocomplete ajax動態獲取資料

</script> 

方法二:前臺資料
<script>
var info = [{"address_city" : "南通","name" : "mm"},  {"address_city" : "南通","name" : "cc"}];
$().ready(function() {
        $("#TReceiverid").autocomplete(
info,                         {                             max : 12, //列表裡的條目數                             minChars : 0, //自動完成啟用之前填入的最小字元                             width : 271, //提示的寬度,溢位隱藏                             scrollHeight : 300, //提示的高度,溢位顯示滾動條                             matchContains : true, //包含匹配,就是data引數裡的資料,是否只要包含文字框裡的資料就顯示                             autoFill : false, //自動填充                             formatItem : function(row, i, max) {//下拉列表裡面顯示的資訊                                 return i + '/' + max + ':"' + row.name + '"['                                         + row.address_city     + ']';                             },                             formatMatch : function(row, i, max) {                                 return row.name;//匹配的欄位                             },                             formatResult : function(row) {                                 return row.name;//選擇之後,input中填充的欄位                             }                         }).result(function(event, row, formatted) {                     alert(row.name);                     $("#s_city").val(row.address_city);              
                }); }); </script>

方法三:後臺資料
<script>

$().ready(function() {
       
 var senders ="";         $.ajax({             url:"<%=basePath%>/members/searchname.do",             method:"get",             dataType: "json",             success:function(data){                 $.each(data.sender, function(index, obj) {//拼接成json資料                             senders = senders + ',{"address_city" : "'                                     + obj.address_city + '","name" : "'                                     + obj.name + '"}';                         });                         senders = '['+senders.substring(1)+ ']';

                        $("#TSenderid").autocomplete(                                 eval(senders),
                                {                                     max : 12, //列表裡的條目數                                     minChars : 0, //自動完成啟用之前填入的最小字元                                     width : 271, //提示的寬度,溢位隱藏                                     scrollHeight : 300, //提示的高度,溢位顯示滾動條                                     matchContains : true, //包含匹配,就是data引數裡的資料,是否只要包含文字框裡的資料就顯示                                     autoFill : false, //自動填充                                     formatItem : function(row, i, max) {                                         return i + '/' + max + ':"' + row.name + '"['                                                 + row.address_city + ']';                                     },                                     formatMatch : function(row, i, max) {                                         return row.name;//匹配的欄位                                     },                                     formatResult : function(row) {                                         return row.name;//選擇之後,input中填充的欄位                                     }                                 }).result(function(event, row, formatted) { 
                                     alert(row.name);                                $("#s_city").val(row.address_city);       
                        });                     }                 });
}); </script>
//還有一種ajax得,下次更