大家好,並且提前感謝幫助我。首先,如果我在這裏使用錯誤的短語,表示歉意。我不擔心這裏的語法,只是讓這個工作。現在,這個問題:我有Django的串行輸出以下JSON:在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的的自動完成功能是
$(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的一般noob,所以請原諒格式錯誤。無論如何,這裏的問題是,雖然這實際上提出請求和自動完成功能,但$ .each(item.fields.translation,function(i)item.fields.translation [i] .fields.word})返回自動完成列表中的[object,Object]。如果我通過alert()輸出它,它會返回正確的值。如果我只是使用.append行中的item.fields.translation [0] .fields.word,它會輸出該值。但由於某種原因,當我要求它做我想做的事情時,我得到[object Object] 因此,任何人都知道我做錯了什麼?提前感謝!
第一個例子正是我所期待的。我不知道推動什麼(就像我說過的,對js總是不喜歡),所以你,我的朋友,是我的新英雄。謝謝謝謝謝謝!蝙蝠工作:D – Nik