【发布时间】:2013-08-01 01:12:05
【问题描述】:
我正在尝试使用 Twitter 预先输入,但我遇到了问题。我不知道 typeahead 如何将字符串传递给服务器。是通过 GET 参数吗?如果有,参数的名称是什么?
【问题讨论】:
-
当我用谷歌搜索
typeahead remote example这个页面时:twitter.github.io/typeahead.js/examples
标签: typeahead
我正在尝试使用 Twitter 预先输入,但我遇到了问题。我不知道 typeahead 如何将字符串传递给服务器。是通过 GET 参数吗?如果有,参数的名称是什么?
【问题讨论】:
typeahead remote example 这个页面时:twitter.github.io/typeahead.js/examples
标签: typeahead
通过 GET 参数最简单,您可以选择所需的任何参数。
在 JS 中:
$('#search').typeahead({
name: 'Search',
remote: '/search.php?query=%QUERY' // you can change anything but %QUERY, it's Typeahead default for the string to pass to backend
});
在 PHP(或您拥有的任何后端)中:
$query = $_GET['query'];
希望您了解基本概念。
【讨论】:
您可能想要考虑这样的事情,这是一个非常基本的远程数据源示例。本例中的get参数为'q'
// Get your data source
var dataSource = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'path/to/your/url/json/datasource/?q=%QUERYSTRING',
wildcard: '%QUERYSTRING'
}
});
// initialize your element
var $typehead = $('#form input').typeahead(null, {
source: dataSource
});
// fire a select event, what you want once a user has selected an item
$typehead.on('typeahead:select', function(obj, datum, name) {
//your code here
});
////////////////////////////////////
# in python (django) we get a query string using the request object passed through a view like this
query = request.GET.get('q') or ""
//the caveat [or ""] is just to prevent null exceptions
///////////////////////////////////
# using php
$query = ($_GET['q']) ? $_GET['q'] : "";
【讨论】: