2014-02-16 2 views
0

위해 여기 내 코드입니다,하는 HTML DIV를 popolate하는 JSON 문자열을 사용하려고하지만, 작동하지 않는 것 같다?는 JSON popuplate의 HTML의 DIV

json으로는 PHP를 통해 생성되고이 잘 작동된다. 감사합니다.

$(document).ready(function(e) { 
    var Currencies = function(){ 
     $.ajax({ 
      url:"js/php/users.php", 
      success:function(data){ 
       return{ 
        getMyJson: function(data){ 
         return(data); 
        } 
       } 
      } 
     });  
    }(); // execute the function when the MyObj variable is initialized. 
}); 
$('#autocomplete').autocomplete({ 
    lookup: Currencies.getMyJson(), 
    onSelect: function (suggestion){ 
     var thehtml = '<strong>Currency Name:</strong> ' + suggestion.nome + ' <br> '; 
     $('#outputcontent').html(thehtml); 
    } 
}); 
+0

당신은 할 수 없어 ... 같은 비동기 방식에서 값을 반환 아약스 호출 ... http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call –

+0

약간 논외 볼 수 있지만, http://rog.ie/blog/ dugjs-A-JSONP - 투 - HTML 스크립트가 – roman

답변

0

이동이 코드

$('#autocomplete').autocomplete({ 
    lookup: Currencies.getMyJson(), 
    onSelect: function (suggestion){ 
     var thehtml = '<strong>Currency Name:</strong> ' + suggestion.nome + ' <br> '; 
     $('#outputcontent').html(thehtml); 
    } 
}); 

$(document).ready(function(e) { 
+0

JSON 데이터와 HTML을 채우기에 매우 깨끗한 솔루션 당신에게 @Arun PJ 감사입니다 ohny하지만. 내가 경고가 – Priya

1

거의 관찰

  • Currencies 객체는 DOM 준비 처리기에 지역이므로 외부 액세스 할 수 없습니다에 그것. DOM 준비 핸들러 내에서 autocomplete 플러그인 초기화를 이동하십시오.
  • 당신은 그런 비동기 방식에서 값을 반환 할 수 없습니다 - How to return the response from an AJAX call

는 시도를 참조하십시오

$(document).ready(function (e) { 
    var Currencies = { 
     getMyJson: function (callback) { 
      $.ajax({ 
       url: "js/php/users.php", 
       success: function (data) { 
        callback(data) 
       } 
      }); 
     } 
    }; // execute the function when the MyObj variable is initialized. 

    Currencies.getMyJson(function (data) { 
     $('#autocomplete').autocomplete({ 
      lookup: data, 
      onSelect: function (suggestion) { 
       var thehtml = '<strong>Currency Name:</strong> ' + suggestion.nome + ' <br> '; 
       $('#outputcontent').html(thehtml); 
      } 
     }); 
    }) 
}); 
+0

작동하지 않습니다 여전히 @zzlalani 감사하지만 – Priya

+0

@Priya 당신이 $ ('# 자동 완성') '전()는 경고를 추가 할 수 있습니다 올바른지 콜백 (데이터) 후 출력 서면 경고 (데이터를) 확인 자동 완성 ({'를 작업하고 있는지 여부를 확인하지 않습니다 <내가 일을 doesen't 이유 –

+0

내가 그것을했다, 출력이 너무 ... 아직도 올바른 인식하지 수 불리는지고 있습니다. < – Priya