jQuery的自动完成(远程) - 例如 [英] jQuery Autocomplete (Remote) - example
问题描述
我真的希望避免发布新的问题,但我找不到jQuery的自动完成远程功能,包括调用页面和搜索页面的功能的例子。 jQueryUI的演示和放大器;文档部分不包括的search.php
I was really hoping to avoid posting a new question, but I cannot find a functioning example of the jQuery Autocomplete Remote feature that includes both the calling page and the "search" page. The jQueryUI "Demos & Documentation" section doesn't include the source of "search.php"
我试过几十个组合,但这里是我开始的:
I've tried dozens of combinations, but here's what I started with:
<style>
.ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
</style>
<script>
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#birds" ).autocomplete({
source: "search.php",
minLength: 1,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>
<div class="demo">
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds" />
</div>
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>
</div>
和search.php中:
and search.php:
$conn = mysql_connect("localhost", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE", $conn);
$q = strtolower($_GET["birds"]);
$query = mysql_query("select FIELD from TABLE where FIELD like '%$q%'");
while ($row = mysql_fetch_array($query)) {
echo json_encode($row);
}
有没有人有code段,介绍了这个等式的两边,他们可以分享?非常感谢任何帮助,您可以提供。
Does anyone have code snippets that show both sides of this equation that they can share? Thanks so much to any help you can provide.
推荐答案
下面是正确code为search.php中:
Here is the correct code for search.php:
$conn = mysql_connect("localhost", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE", $conn);
$q = strtolower($_GET["term"]);
$return = array();
$query = mysql_query("select FIELD from TABLE where FIELD like '%$q%'");
while ($row = mysql_fetch_array($query)) {
array_push($return,array('label'=>$row['FIELD'],'value'=>$row['FIELD']));
}
echo(json_encode($return));
一些关键点
- 字项是无处由jQueryUI的给出的样本调用页面,但是这是使用的查询字符串名称
- 您必须创建值的数组,然后JSON EN code返回前
- The word term is nowhere in the sample calling page given by jqueryui, but this is the Querystring name used
- You must create an array of the value, and then json encode before returning
我希望这有助于一些人出未来!
I hope this helps some folks out in the future!
这篇关于jQuery的自动完成(远程) - 例如的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!