2015-01-05 6 views
0

에서 항목을 선택 얻을 수 없다, 나는 외부 PHP를 통해 교외/우편 번호의 목록을 가져옵니다 다음 코드를했다 : 나는 목록을 자동 완성 자체가 잘 작동JQuery와 자동 완성 UI 위젯을 사용하여 JQuery와 자동 완성

<script> 

    $(function() { 
     $("#suburb").autocomplete({ 

      minLength:3, //minimum length of characters for type ahead to begin 
      source: function (request, response) { 
       $.ajax({ 
        type: 'POST', 
        url: 'resources/php/helpers.php', //your server side script 
        dataType: 'json', 
        data: { 
         suburb: request.term 
        }, 

        success: function (data) { 
         //if multiple results are returned 
         if(data.localities.locality instanceof Array) 
          response ($.map(data.localities.locality, function (item) { 
           return { 
            label: item.location + ', ' + item.postcode, 
            value: item.location + ', ' + item.postcode 
           } 
          })); 
         //if a single result is returned 
         else 
          response ($.map(data.localities, function (item) { 
           return { 
            label: item.location + ', ' + item.postcode, 
            value: item.location + ', ' + item.postcode 
           } 
          })); 
        }, 

        select: function (event, ui) { 
         alert("SELECT"); 
         $('#postCode').val("POSTCODE"); 
         return true; 
        } 
       }); 
      } 

     }); 
    }); 
</script> 

를 얻을 수 하지만 'select'부분이 작동하지 않습니다. 즉, 다른 입력 텍스트 값을 선택된 값으로 설정해야하지만 위 코드에서도 경고 대화 상자가 표시되지 않습니다. 나는 혼란 스럽습니다. 그래서 여기에서 내가 뭘 잘못했는지 모르겠습니다.

답변

1

select 함수는 ajax 메서드로 전송되는 개체 외부에 있어야합니다.

이 시도 :

$(function() { 
    $("#suburb").autocomplete({ 

     minLength:3, //minimum length of characters for type ahead to begin 
     source: function (request, response) { 
      $.ajax({ 
       type: 'POST', 
       url: 'resources/php/helpers.php', //your server side script 
       dataType: 'json', 
       data: { 
        suburb: request.term 
       }, 

       success: function (data) { 
        //if multiple results are returned 
        if(data.localities.locality instanceof Array) 
         response ($.map(data.localities.locality, function (item) { 
          return { 
           label: item.location + ', ' + item.postcode, 
           value: item.location + ', ' + item.postcode 
          } 
         })); 
        //if a single result is returned 
        else 
         response ($.map(data.localities, function (item) { 
          return { 
           label: item.location + ', ' + item.postcode, 
           value: item.location + ', ' + item.postcode 
          } 
         })); 
       } 
      }); 
     }, 

     select: function (event, ui) { 
      alert("SELECT"); 
      $('#postCode').val("POSTCODE"); 
      return true; 
     }    

    }); 
});