【发布时间】:2024-05-02 05:15:02
【问题描述】:
我正在使用 Bootstrap 3.3.6、JQuery 1.11.0、Bootstrap3-typeahead 4.0.2 和 bootstrap-tagsinput 0.8.0。
这是我的代码:
$(function() {
$('.tagsinput-typeahead').tagsinput({
confirmKeys: [13],
itemValue: 'value',
itemText: 'text',
typeahead: {
displayKey: 'text',
afterSelect: function(val) { this.$element.val(""); },
source: function (query) {
return jQuery.get("typeaheadSource.php?q=" + query);
// return jQuery.post("typeaheadSource.php?q=" + query); // I tried both get and post
}
}
});
});
<div class="form-group">
<label class="control-label col-md-3">Job Number(s) </label>
<div class="col-md-8">
<select multiple="multiple" class="form-control tagsinput-typeahead" name="typeahead" id="typeahead" ></select>
</div>
</div>
这是我的 typeaheadSource.php:
if (isset($_REQUEST['q'])) {
$query = $_REQUEST['q'];
$sql = "SELECT jobno, jobname FROM jobs WHERE jobno LIKE '%" . $query . "%' ORDER BY jobno DESC LIMIT 20";
$result=db_query($sql);
while($row = db_nextrow($result)) {
$array[] = array('value' => $row['jobno'], 'text' => $row['jobname']);
}
if (isset($array)) {
echo json_encode($array);
}
}
如果我直接运行 typeaheadSource.php,输出如下所示:
[{"value":"2012006.00","text":"Monterey Hotel Investigation"},{"value":"2006142.00","text":"Ollendorff Residence"},{"value":"2006141.01","text":"MLK Student Union Peer Review Expanded Scope"},{"value":"2006141.00","text":"MLK Student Union Peer Review"}]
如果我将该输出作为源代码放入我的代码中,一切都会很好。
$('.tagsinput-typeahead').tagsinput({
confirmKeys: [13],
itemValue: 'value',
itemText: 'text',
typeahead: {
displayKey: 'text',
afterSelect: function(val) { this.$element.val(""); },
source: [{"value":"2012006.00","text":"Monterey Hotel Investigation"},{"value":"2006142.00","text":"Ollendorff Residence"},{"value":"2006141.01","text":"MLK Student Union Peer Review Expanded Scope"}]
}
});
但是当我的源调用我的远程 URL 时,没有显示预输入并且我没有收到任何错误。
source: function (query) {
return jQuery.get("typeaheadSource.php?q=" + query);
}
是否有人对如何使用远程 URL 查询 MySQL 数据库有任何建议或建议?
【问题讨论】:
标签: jquery mysql twitter-bootstrap typeahead.js bootstrap-tags-input