javascript - 远程数据源在 Jquery Mobile 自动完成中如何工作?

标签 javascript jquery jquery-mobile autocomplete

我正在阅读此页http://demos.jquerymobile.com/1.4.5/listview-autocomplete-remote/并且似乎没有关于远程数据源的外观或工作方式的文档。

在该页面中,您可以看到远程数据源的 JS 示例是 http://gd.geobytes.com/AutoCompleteCity,但访问该 URL 不会显示任何内容。

有谁知道它应该如何构造以便与自动完成页面一起使用?

最佳答案

数据以 JSON 形式作为数组返回。在文档的示例中,如果你看一下 JS 代码:

$.ajax({
dataType: "jsonp"
...
.then(function(response) {  // <-- this expects response as a JSON object
$.each(response,function(i,val){  // <-- each iterates over the array, i is the index, val is the value
    html+="<li>"+val+"</li>"; //<-- creates the HTML for the autocomplete

})

事实上,您可以使用 Chrome 上的 Google 开发者工具来查看响应。在示例中,如果您写“Ale”,您会得到:

jQuery1102023577826283872128_1446566971852(["Aledo, IL, United States","Aledo, MU, Spain","Aledo, TX, United States","Aleknagik, AK, United States","Aleppo, HL, Syria","Aleppo, PA, United States","Alert, NT, Canada","alesund, MR, Norway","Alex, OK, United States","Alexander City, AL, United States","Alexander, AR, United States","Alexander, IA, United States","Alexander, IL, United States","Alexander, KS, United States","Alexander, NC, United States","Alexander, ND, United States","Alexander, NY, United States","Alexandra Headland, QL, Australia","Alexandra Hills, QL, Australia","Alexandra, GT, South Africa"]);

您可以尝试使用变量 q 将查询作为参数传递的示例 URL:

http://gd.geobytes.com/AutoCompleteCity?q=abe

关于javascript - 远程数据源在 Jquery Mobile 自动完成中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503153/

相关文章:

javascript - 如何将唯一的事件监听器绑定(bind)和取消绑定(bind)到同一对象?

javascript - 如何在所有具有特定类的 dom 元素上添加 EventListener?

javascript - 正则表达式 : match with optional wrapping text

javascript - 数组不传递数据

javascript - 更改按钮 onclick 中的字形

iphone - jQuery 移动页面在 Safari 移动中 super 大

javascript - 如何使用 javascript 或 jQuery 引用 csv 或 json 文件中的项目动态地将项目和属性添加到 HTML

javascript - JS 切换不起作用

cordova - Jquery Mobile 的复选框在 iOS 上使用 PhoneGap 更改触发两次(但在 Android 上工作)

internet-explorer - Adsense 广告未显示在 IE 中(IE浏览器)