在 jquery-ui 自动完成中处理来自自定义源的数据
我正在尝试使用 jquery-ui 在搜索字段中自动完成。由于搜索取决于另一个表单字段的值,因此我对源使用回调。我可以看到请求已正确发送。我的远程脚本返回一个简单的字符串数组,但此时我无法让它工作。下拉列表永远不会构建。谁能告诉我为什么?这是代码:
$(document).ready(function(){
$("#species").autocomplete({
source: function( request, response ) {
$.ajax({
url: "/includes/species-ajax.cfm",
dataType: "jsonp",
data: {
term: request.term,
searchBy : function() {
var sb = $("#searchBy_hidden").val();
return (sb ? sb : 'common_name'); }
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item.name,
value: item.name
}
}));
}
});
}});
});
<input type="hidden" name="searchBy_hidden" id="searchBy_hidden" value="common_name" />
Enter the name of a species: <input type="textbox" size="30" id="species" />
谢谢,
I am trying to use jquery-ui for autocompletion in a search field. Because the search depends on the value of another form field, I'm using a callback for the source. I can see that the request is sent correctly. My remote script returns a simple array of strings and it's at that point that I can't get it to work. The dropdown list is never built. Can anyone tell me why? Here's the code:
$(document).ready(function(){
$("#species").autocomplete({
source: function( request, response ) {
$.ajax({
url: "/includes/species-ajax.cfm",
dataType: "jsonp",
data: {
term: request.term,
searchBy : function() {
var sb = $("#searchBy_hidden").val();
return (sb ? sb : 'common_name'); }
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item.name,
value: item.name
}
}));
}
});
}});
});
<input type="hidden" name="searchBy_hidden" id="searchBy_hidden" value="common_name" />
Enter the name of a species: <input type="textbox" size="30" id="species" />
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
dataType
更改为'json'
,而不是'jsonp'
Try changing your
dataType
to'json'
, not'jsonp'