2012-05-21 2 views
2

성의 마지막 항목 만 얻는 것 같습니다. 나는 하루 종일 이것을 보냈고 누군가 내가 무엇을 놓쳤는지를 볼 수 있는지 궁금해하고있었습니다. , 당신은 당신의 결과를 덮어하고 모든 반복에 .html()를 사용하여 당신에게API에서받은 json을 반복합니다.

<!doctype html> 
<html> 

    <head> 
     <meta charset="utf-8"> 
     <title>Untitled Document</title> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $.ajax({ 
        url: "http://testsite.legisconnect.com/list.php", 
        dataType: "json", 
        success: function (data) { 
         $.each(data.response.legislators, function (i, item) { 
          $('#here').html(item.legislator.lastname); 
         }); 
        } 
       }); 
      }); 
     </script> 
    </head> 

    <body> 
     <div id="here"></div> 
    </body> 

</html> 

JSON

{ 
    "response": { 
     "legislators": [{ 
      "legislator": { 
       "website": "http://ackerman.house.gov/", 
       "fax": "202-225-1589", 
       "govtrack_id": "400003", 
       "firstname": "Gary", 
       "chamber": "house", 
       "middlename": "L.", 
       "lastname": "Ackerman", 
       "congress_office": "2111 Rayburn House Office Building", 
       "eventful_id": "", 
       "phone": "202-225-2601", 
       "webform": "http://www.house.gov/writerep", 
       "youtube_url": "http://www.youtube.com/RepAckerman", 
       "nickname": "", 
       "gender": "M", 
       "district": "5", 
       "title": "Rep", 
       "congresspedia_url": "http://www.opencongress.org/wiki/Gary_Ackerman", 
       "in_office": true, 
       "senate_class": "", 
       "name_suffix": "", 
       "twitter_id": "repgaryackerman", 
       "birthdate": "1942-11-19", 
       "bioguide_id": "A000022", 
       "fec_id": "H4NY07011", 
       "state": "NY", 
       "crp_id": "N00001143", 
       "official_rss": "", 
       "facebook_id": "RepAcherman", 
       "party": "D", 
       "email": "", 
       "votesmart_id": "26970" 
      } 
     }, { 
      "legislator": { 
       "website": "http://adams.house.gov/", 
       "fax": "202-226-6299", 
       "govtrack_id": "412414", 
       "firstname": "Sandra", 
       "chamber": "house", 
       "middlename": "", 
       "lastname": "Adams", 
       "congress_office": "216 Cannon House Office Building", 
       "eventful_id": "", 
       "phone": "202-225-2706", 
       "webform": "", 
       "youtube_url": "http://www.youtube.com/RepSandyAdams", 
       "nickname": "", 
       "gender": "F", 
       "district": "24", 
       "title": "Rep", 
       "congresspedia_url": "http://www.opencongress.org/wiki/Sandy_Adams", 
       "in_office": true, 
       "senate_class": "", 
       "name_suffix": "", 
       "twitter_id": "RepSandyAdams", 
       "birthdate": "1956-12-14", 
       "bioguide_id": "A000366", 
       "fec_id": "H0FL24049", 
       "state": "FL", 
       "crp_id": "N00030926", 
       "official_rss": "", 
       "facebook_id": "", 
       "party": "R", 
       "email": "", 
       "votesmart_id": "31041" 
      } 
     }, { 
      "legislator": { 
       "website": "http://aderholt.house.gov/", 
       "fax": "202-225-5587", 
       "govtrack_id": "400004", 
       "firstname": "Robert", 
       "chamber": "house", 
       "middlename": "B.", 
       "lastname": "Aderholt", 
       "congress_office": "2264 Rayburn House Office Building", 
       "eventful_id": "", 
       "phone": "202-225-4876", 
       "webform": "http://aderholt.house.gov/?sectionid=195&sectiontree=195", 
       "youtube_url": "http://www.youtube.com/RobertAderholt", 
       "nickname": "", 
       "gender": "M", 
       "district": "4", 
       "title": "Rep", 
       "congresspedia_url": "http://www.opencongress.org/wiki/Robert_Aderholt", 
       "in_office": true, 
       "senate_class": "", 
       "name_suffix": "", 
       "twitter_id": "Robert_Aderholt", 
       "birthdate": "1965-07-22", 
       "bioguide_id": "A000055", 
       "fec_id": "H6AL04098", 
       "state": "AL", 
       "crp_id": "N00003028", 
       "official_rss": "", 
       "facebook_id": "RobertAderholt", 
       "party": "R", 
       "email": "", 
       "votesmart_id": "441" 
      } 
     }] 
    } 
} 

답변

3

성공 처리기에서 컬렉션을 반복하면서 매번 innerHTML을 새로운 항목으로 바꿉니다. 당신은 루프의 끝에 도달 할 때 다른 사람의 모든 반복적으로 대체되었습니다로

   success: function (data) { 
        $.each(data.response.legislators, function (i, item) { 
         $('#here').html(item.legislator.lastname); 
        }); 
       } 

따라서, 가장 최근의 LASTNAME이 삽입됩니다.

아래 예제와 같이 추가 하시겠습니까? 또는 매번 다른 요소에 이름을 삽입 할 수 있습니까?

   success: function (data) { 
        $.each(data.response.legislators, function (i, item) { 
         $('#here').append('<span>' + item.legislator.lastname + '</span>'); 
        }); 
       } 
+0

나는 당신을 사랑합니다 ... 나는 당신을 문자 그대로 사랑합니다. – dianeinflorida

+0

@ dianeinflorida - lol. 환영합니다. 기꺼이 도와 드리겠습니다. – jmort253

+0

@ dianeinflorida : [답변을 수락 함으로 표시하십시오] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)를 반드시 확인하십시오. –

4

감사드립니다. 따라서 마지막 항목 만 표시됩니다. 대신 .append()으로 시도하십시오.