Jquery 搜索示例:
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$("#tags").autocomplete({
source: availableTags
});
});
</script>
<div class="demo">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
</div>
<!-- End demo -->
<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source- option.</p>
</div>
<!-- End demo-description -->
基本上有一个带有自动完成内容的变量,这很好,除了我需要一些可能更复杂的东西。我不需要从 var/xml/sql 提供列表,而是需要从第三方 php 脚本发出的 echo 中获取。
该 php 脚本将根据查询回显相应的信息。即:用户搜索 customsearch.php?q=Lemons 它将回显“Pineapples”。
有人可以帮助我吗?
最佳答案
基于您的其他question ,我假设您正在进行 AJAX 调用来获取搜索结果。将它们加载到数组中并在示例中替换它:
<script>
function GetSearchResults(){
// make your ajax call here
$.ajax({
async: false,
url: 'customsearch.php?q=Lemons',
success: function(data) {
var availableTags = [];
// build an array from the response data here
$( "#tags" ).autocomplete({
source: availableTags
});
}
});
}
$(function() {
var availableTags = GetSearchResults();
});
</script>
<div class="demo">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
</div><!-- End demo -->
<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source- option.</p>
</div><!-- End demo-description -->
理想情况下,您不会将 async 设置为 false,但如果您不熟悉回调,我会尽力不让您的大脑爆炸。
关于java - Jquery + PHP : Grabbing the autocomplete strings from a third party php script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8421357/