在 Jquery 中迭代 Json
大家好,提前感谢您对我的帮助。首先,如果我在这里使用了错误的短语,我深表歉意。我不太担心这里的语法,只是让它发挥作用。现在,问题是:我有 django 序列化器输出以下 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 的完全菜鸟一般来说,请原谅任何明显的格式错误。无论如何,这里的问题是,虽然这确实发出了请求,并且自动完成功能起作用,但 $.each(item.fields.translation, function(i) item.fields.translation[i].fields.word}) 返回自动完成列表中的[对象,对象]。如果我通过alert()输出它,它会返回正确的值。如果我只在 .append 行中使用 item.fields.translation[0].fields.word ,它会输出该值。但由于某种原因,当我要求它做我想做的事情时,我得到 [object Object] 那么,有人知道我做错了什么吗?提前非常感谢!
Hi everyone and thanks in advance for helping me out. first of all, apologies if I use the wrong phrase here. I'm not so worried about syntax here, just getting this to work. Now, the issue: I have the django serializer outputting the following 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
}
}
]
What I'd like to do is get to fields.translation.fields.words and thus the Jquery for the Autocomplete is
$(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 );
};
});
I'm a total noob to jquery and javascript in general, so pardon any glaring formatting errors. Anyways, The problem here is, while this does actually make the request, and the autocomplete functions, the $.each(item.fields.translation, function(i) item.fields.translation[i].fields.word}) returns [object, Object] in the autocomplete list. If I have it output via alert() it returns the correct values. If I just use item.fields.translation[0].fields.word in the .append line, it outputs the value. But for some reason, when i ask it to do what I want it to do, I get [object Object]
So, anyone have any idea what I'm doing wrong? Thanks a ton in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将变量
tran
设置为等于$.each()
函数,其中 返回jQuery
。这就是为什么它最终成为一个对象。我不清楚你到底想做什么。您正在循环
item.fields.translation
数组中的项目,但最终执行附加操作,就好像此循环只返回单个字符串一样。但是item.fields.translation
的数组中有两个项目,所以...您可以构建这样的数组:不确定这是否有帮助。如果您澄清您期望
tran
是什么,那么我可以提供进一步的帮助。附注:您可以将当前迭代中项目的值(而不仅仅是其索引/键)传递给
$.each()
内的函数。例如:You're setting your variable
tran
equal to the$.each()
function, which returnsjQuery
. That's why it ends up being an object.I'm not clear on what exactly you're trying to do. You're looping over the items in the
item.fields.translation
array, but ultimately doing an append as if this loop should just return a single string. Butitem.fields.translation
has two items in its array, so... You could build an array like this:Not sure if that helps. If you clarify what you are expecting
tran
to be, then I could help further.And a side note: you can pass the value of the item in the current iteration--rather than just its index/key--to the function inside
$.each()
. For example: