[英]jquery autocomplete returns [object Object]
I've been beating my head against the wall trying to get this work. 我一直在努力地争取完成这项工作。 I'm not very good at this so i'm sure i'm missing something simple.
我不是很擅长此事,所以我确定我缺少一些简单的东西。 I have searched the knowledgebase but i haven't found something that helps and i'm pretty sure there's a better way of accomplishing this.
我已经搜索了知识库,但没有找到任何有用的方法,并且我敢肯定,有更好的方法可以实现这一目标。
I'm trying to have my input field autocomplete but it returns [object Object]. 我试图让我的输入字段自动完成,但它返回[object Object]。 Any help would be greatly appreciated.
任何帮助将不胜感激。 Thanks!
谢谢!
jquery: jQuery的:
<script>
$('#client_id').autocomplete({
source: function( request, response ) {
$.ajax({
type: "POST",
url: '/slips/suggest',
dataType: "json",
data: {
user_input: request.term
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item,
value: item
}
}));
}
});
},
autoFocus: true,
minLength: 3
});
</script>
json output: json输出:
[{"label":"Doe, John","value":"1"},{"label":"Doe, Jane","value":"2"},{"label":"Doe, Jack","value":"3"},{"label":"Doe, Jake","value":"4"},{"label":"Doe, Jim","value":"5"}]
html: HTML:
<div style="height: 20px;float: left;display: inline-block;padding-top:15px;">
<span class="new_slip">Client</span>
<input type="text" id="client_id" name="client_id" style="width:100px;" />
</div>
The data
provided in the response is already in one of the structures that .autocomplete()
expects : 响应中提供的
data
已经在.autocomplete()
期望的结构之一中 :
- Array
排列
- ...
...
- An array of objects with label and value properties:
[ { label: "Choice1", value: "value1" }, ... ]
具有标签和值属性的对象数组:
[ { label: "Choice1", value: "value1" }, ... ]
So, the $.map()
isn't necessary, in this case. 因此,在这种情况下,不需要
$.map()
。
// ...
success: response
// ...
The [object Object]
you're seeing is the default .toString()
result for Object
s. 在
[object Object]
你看到的是默认.toString()
导致的Object
秒。
console.log({}.toString());
// "[object Object]"
And, comes from assigning each item
, which are all Object
s, as the label
and value
: 并且,通过将所有都是
Object
的item
分配为label
和value
:
var data = [ {"label":"Doe, John","value":"1"} ];
data = $.map( data, function ( item ) {
return {
label: item,
value: item
}
});
console.log(data);
/*
[
{
label: { label: "Doe, John", value: 1 },
value: { label: "Doe, John", value: 1 }
}
]
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.