2017-08-08 184 views
0

我想在我的asp.net應用程序的用戶名上使用自動完成。我正在從數據庫檢索名稱&關聯的ID。我已檢查它是否正在工作,以便我的後端代碼將數據返回到前端。但它不會顯示在自動填充字段中。這裏是我的腳本:jQuery自動完成不顯示數據

<script type="text/javascript"> 
     $(document).ready(function() { 
      autocomplete(); 
     }); 
     function autocomplete() { 
      $("[id$=txt_name]").autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         url: '<%=ResolveUrl("../Services/AutoComplete.asmx/getCustomerDetails") %>', 
         data: "{ 'prefix': '" + request.term + "'}", 
         dataType: "json", 
         type: "POST", 
         contentType: "application/json; charset=utf-8", 
         success: function (data) { 
          response($.map(data.d, function (item) { 
           return { 
            label: item.split('-')[0], 
            val: item.split('-')[1] 
           } 
          })) 
         }, 
         error: function (response) { 
          alert(response.responseText); 
         }, 
         failure: function (response) { 
          alert(response.responseText); 
         } 
        }); 
       }, 
       select: function (e, i) { 
        $("[id$=hfCustId]").val(i.item.val); 
        $("[id$=txt_name]").change(); 

        return false; 
       }, 
       minLength: 1 
      }); 
     }; 
    </script> 
+0

分享您的服務代碼。 – Ravikumar

回答

0

它應該是這樣的

response($.map(data.d, function (item) { 
            return { 
             label: item.split('-')[0], 
             value: item.split('-')[1] 
            } 
           })) 

<script type="text/javascript"> 
     $(document).ready(function() { 
      autocomplete(); 
     }); 
     function autocomplete() { 
      $("[id$=txt_name]").autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         url: '<%=ResolveUrl("../Services/AutoComplete.asmx/getCustomerDetails") %>', 
         data: "{ 'prefix': '" + request.term + "'}", 
         dataType: "json", 
         type: "POST", 
         contentType: "application/json; charset=utf-8", 
         success: function (data) { 
          response($.map(data.d, function (item) { 
           return { 
            label: item.split('-')[0], 
            value: item.split('-')[1] 
           } 
          })) 
         }, 
         error: function (response) { 
          alert(response.responseText); 
         }, 
         failure: function (response) { 
          alert(response.responseText); 
         } 
        }); 
       }, 
       select: function (e, i) { 
        $("[id$=hfCustId]").val(i.item.val); 
        $("[id$=txt_name]").change(); 

        return false; 
       }, 
       minLength: 1 
      }); 
     }; 
    </script>