2011-10-05 6 views
1

안녕하세요 모두 도와 주셔서 미리 감사드립니다. 우선 내가 잘못된 말을 사용하면 사과드립니다. 나는 구문에 대해 그렇게 걱정하지 않는다. 단지 이것을 작동 시키도록한다. 이제 문제 : 내가하고 싶은 무엇Jquery에서 Json 반복하기

[ 
    { 
     "pk": 11262, 
     "model": "dict.words", 
     "fields": { 
      "lang": "KO", 
      "incorrect": null, 
      "sug_translation": [ 
       3215 
      ], 
      "word": "\uc0dd\uac01\ud558\ub2e4", 
      "definition": [], 
      "pos": "VE", 
      "phrase": [], 
      "translation": [ 
       { 
        "pk": 1, 
        "model": "dict.words", 
        "fields": { 
         "word": "comprender" 
        } 
       }, 
       { 
        "pk": 6028, 
        "model": "dict.words", 
        "fields": { 
         "word": "entender" 
        } 
       } 
      ], 
      "incomplete": null 
     } 
    } 
] 

이 fields.translation.fields.words에 도착하고, 따라서 자동 완성에 대한 JQuery와

입니다 : 나는 다음과 같은 JSON을 출력하는 장고 시리얼을
$(function() { 

      $("#query_form").autocomplete({ 
        minLength: 2, 
        source: 'http://127.0.0.1:8000/json_results/', 
        focus: function(event, ui) { 
          $("#query_form").val(ui.item.word); 
          return false; 
        }, 
      select: function(event, ui) { 
        $.get ('http://127.0.0.1:8000/json_detail/', 
           { 
           item: ui.item.pk 
           }, 
           function(data) { 
           $('#query_result').prepend(data); 
           }); 
        $("#query_form").val(ui.item.word); 
        return false; 
      } 
    }) 
    .data("autocomplete")._renderItem = function(ul, item) { 
         var tran = $.each(item.fields.translation, function(i){item.fields.translation[i].fields.word}) 
       return $("<li></li>") 
         .data("item.autocomplete", item) 
         .append("<a>" + item.fields.word + tran + "</a>") 
         .appendTo(ul); 
    }; 
}); 

저는 jquery와 javascript에 일반적으로 멍청한 의견을 보내고 있습니다. 따라서 글 복사 오류를 용서해주십시오. 어쨌든, 여기 문제는 실제로 요청을 만드는 동안, 그리고 자동 완성 함수 인 $ .each (item.fields.translation, function (i) item.fields.translation [i] .fields.word})가 리턴합니다. [개체, 개체] 자동 완성 목록에. alert()를 통해 출력하면 올바른 값을 반환합니다. .append 줄에 item.fields.translation [0] .fields.word 만 사용하면 값을 출력합니다. 하지만 웬일인지, 내가 원하는 것을하기 위해 그것을 요구할 때, 나는 [object Object]를 얻는다. 그래서 내가 뭘 잘못하고 있는지 알 수있다. 미리 감사드립니다.

답변

1

변수 tran$.each() 함수 (returns jQuery)와 동일하게 설정하고 있습니다. 그것이 개체가되는 이유입니다.

정확히 무엇을 하려는지 명확하지 않습니다. item.fields.translation 배열의 항목을 반복하지만 궁극적으로이 루프가 단일 문자열을 반환해야하는 것처럼 추가 작업을 수행하고 있습니다. 그러나 item.fields.translation는 배열의 두 항목이있다, 그래서 ...이 같은 배열을 만들 수있다 :

var tran = []; 
$.each(item.fields.translation, function(i){ 
    tran.push(item.fields.translation[i].fields.word); 
}); 
//tran now equals ['comprender','entender'] 

이 도움이되는지 확실하지 않음을. tran이 무엇을 기대하는지 명확하게 설명하면 더 도움이 될 수 있습니다.

사이드 노트 : $.each() 내부 함수의 색인/키가 아닌 현재 반복 항목의 값을 전달할 수 있습니다. 예 :

$.each(item.fields.translation, function(i,v){ 
    //the following is the same as tran.push(item.fields.translation[i].fields.word); 
    tran.push(v.fields.word); 
}); 
+0

첫 번째 예제는 내가 찾고있는 것입니다. 나는 푸시에 대해 전혀 몰랐다. (내가 말했듯이, 멍청한 멍청이가) 당신, 내 친구, 내 새로운 영웅이다. 고마워요 고마워요! 박쥐 오른쪽에서 일 : D – Nik

관련 문제