循环访问数据以显示自动完成建议
Iterate over the data to show Autocomplete suggestions
这是一个在数组中返回结果的API,用于自动建议
例如,如果我查询"谷歌",我会得到如下结果
["google","google maps","google translate","google earth","google images","google docs","google voice","google scholar","google chrome","google calendar",]
你可以在这里自己尝试
这是我用来查询此 API 的代码,但它似乎没有返回结果。
这是我用于它的代码
$(function() {
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "q.php",
dataType: "json",
data: {
"q" : request.term
},
success: function( data ) {
response(data[1]);
}
});
},
minLength: 2
});
});
我不明白我在代码中哪里出错了请纠正我!因为它似乎没有像我想要的那样工作
编辑:从同一服务器访问数据
你忘了在你的小提琴中添加jquery-ui库。但是如果你这样做,代码无论如何都不起作用,因为你无法通过ajax请求访问来自另一个域的数据。仅来自 js 代码执行的同一域。
这可能会有所帮助
$(document).ready(function() {
$.getJSON('http://twitter.com/users/usejquery.json?callback=?', function(json) { //get information about the user usejquery from twitter api
$('#twitter_followers').text(json.followers_count); //get the follower_count from the json object and put it in a span
});
});
寻找称为跨域 ajax 的东西。http://usejquery.com/posts/the-jquery-cross-domain-ajax-guide
您可以在此处阅读有关将 jQuery autocomlete 用于跨域环境的信息: http://1300grams.com/2009/08/17/jquery-autocomplete-with-json-jsonp-support-and-overriding-the-default-search-parameter-q/
此外,您还需要将jquery.autocomplete.js添加到jsFiddle环境中。
相关文章:
- jQuery从html访问数据
- 如何处理在javascript中访问数据结构的两个回调
- 无法使用变量访问数据 JSON
- nodejs API 查询如何访问数据
- 使用jquery循环表行并通过类名访问数据
- 从JavaScript对象访问数据's数组成员变量
- json访问数据未正确显示
- JSON可以't访问数据返回未定义
- AngularJS从另一个数据模型访问数据模型
- 回调访问数据
- RxJs:在flatMapMapLate之前访问数据最新
- 如何使用数据集访问数据属性值
- Chrome 扩展程序无法访问数据存储
- 角度:访问数据
- ExtJS v3 - 从表单面板内的网格面板访问数据
- 从外部 Javascript 文件访问数据
- 使用 AngularJS 从没有键值的 JSON 访问数据值
- Birt:从 beforeFactory 访问数据集的内容
- 访问数据结构中的元素
- 使用数组和对象的组合循环访问数据