【发布时间】:2013-04-17 13:12:05
【问题描述】:
我在使用 jQueryUi 的自动完成组件时遇到了一些问题。不显示带有自动完成建议的列表。
我已经测试了以下代码(来自jQuery UI),尽管 servlet 正在发送 JSON 对象并且“数据”变量正在对其进行记录,但组件仍然没有显示建议列表。
我还尝试了使用简单列表作为源 (like here) 的组件,它运行良好。
你知道会发生什么吗?
<script>
$(function() {
var cache = {};
$( "#bear" ).autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
$.getJSON( "/animals/MaintainMamals?operation=14", request, function( data, status, xhr ) {
cache[ term ] = data;
response( data );
});
}
});
});
</script>
<form>
<div class="ui-widget">
<label for="bear">Bear name (type a piece of name): </label>
<input id="bear" name="bear" class="text ui-widget-content ui-corner-all"/>
</div>
</form>
测试中使用的 Json 对象(我尝试了一个简单的 jSon 构建的东西,它只使用一个引用“name”属性的字符串,结果相同 [bad]):
[
{
"id": 1234567,
"name": "Yogi Bear",
"activity": {
"handler": {
"interfaces": [
{}
],
"constructed": true,
"persistentClass": {},
"getIdentifierMethod": {
"clazz": {},
"slot": 2,
"name": "getCod",
"returnType": {},
"parameterTypes": [],
"exceptionTypes": [],
"modifiers": 1,
"root": {
"clazz": {},
"slot": 2,
"name": "getId",
"returnType": {},
"parameterTypes": [],
"exceptionTypes": [],
"modifiers": 1,
"override": false
},
"override": false
},
"setIdentifierMethod": {
"clazz": {},
"slot": 3,
"name": "setId",
"returnType": {},
"parameterTypes": [
{}
],
"exceptionTypes": [],
"modifiers": 1,
"root": {
"clazz": {},
"slot": 3,
"name": "setId",
"returnType": {},
"parameterTypes": [
{}
],
"exceptionTypes": [],
"modifiers": 1,
"override": false
},
"override": false
},
"overridesEquals": false,
"entityName": "com.zoo.Activity",
"id": 7105,
"initialized": false,
"readOnly": false,
"unwrap": false
}
}
}
]
【问题讨论】:
-
为什么在每个标签中
<后面都有'? -
您的源对象需要具有 label 和/或 value 属性才能使用小部件。您可以转换您的数据,以便小部件可以使用它
-
@Mooseman,是由于防止 stackOverflow 页面将其解释为自己的标签。如果您有提示以便以最佳方式做到这一点,我很乐意知道:)。
-
@AndrewWhitaker,我该怎么做?你有什么建议吗?
标签: jquery-ui jquery jquery-plugins jquery-ui-autocomplete