一如往常,demo和源碼的zip包在文章最后,慢慢欣賞吧!
我覺得我有必要寫這個教程,因為曾經見到的大部分關于自動完成的應用程序都只是給你一個程序源碼包,然后告訴你怎么使用,而不是告訴你它是如何工作的以及為什么這樣做。而知道這些可以讓你對這個插件可以進一步的按自己的需求定制(關于這一點我在我的blog里寫過不少關于其他應用的文章)。
好,我們現在開始。
javascript代碼:
<script src="jquery-1.2.1.pack.js" type="text/javascript"></script>
<script type="text/javascript">
function lookup(inputstring) {
if(inputstring.length == 0) {
// hide the suggestion box.
$(‘#suggestions’).hide();
} else {
$.post("rpc.php", {querystring: ""+inputstring+""}, function(data){
if(data.length >0) {
$(‘#suggestions’).show();
$(‘#autosuggestionslist’).html(data);
}
});
}
} // lookup
function fill(thisvalue) {
$(‘#inputstring’).val(thisvalue);
$(‘#suggestions’).hide();
}
</script>
js的解釋:
好,從上面的代碼看到,我們需要連接到一個叫做rpc.php的文件,這個文件處理所有的操作。
lookup函數使用從文本輸入框中得到的單詞然后使用jquery中ajax的方法post把它傳給rpc.php。
如果輸入字符 ‘inputstring’是‘0’(zero,譯注:在這里是指在搜索框中沒輸入任何內容),建議框就被隱藏,這也很人性化,你想,如果在搜索框中沒有輸入任何東西,你也不期望會出現個建議提示框。
如果輸入框中有內容,我們就得到了這個 ‘inputstring’并傳遞給rpc.php頁面,然后jquery 的$.post()函數被使用,如下:
$.post(url, [data], [callback])
‘callback’部分可以關聯一個函數,這個比較有意思,只有在數據(data)被加載成功的時候才會執行(譯注:此處為意譯,沒看懂原文:<).
如果返回的數據(data)不為空(也就是說,有東西要顯示),那就顯示搜索提示框并且使用返回的數據(data)來代替其中的html代碼。
就這么簡單!
|||
php后臺程序(rpc.php):
如你所知(譯注:不好意思,看王小波就學會了這么個口頭禪),我的php后臺程序都叫做rpc.php(rpc指遠程過程調用),而沒用它實際執行的功能來命名,但是也還不錯了。
// php5 implementation - uses mysqli.
$db = new mysqli(‘localhost’, ‘root’ ,”, ‘autocomplete’);
if(!$db) {
// show error if we cannot connect.
echo ‘error: could not connect to the database.’;
} else {
// is there a posted query string?
if(isset($_post[‘querystring’])) {
$querystring = $_post[‘querystring’];
// is the string length greater than 0?
if(strlen($querystring) >0) {
// run the query: we use like ‘$querystring%’
// the percentage sign is a wild-card, in my example of countries it works like this…
// $querystring = ‘uni’;
// returned data = ‘united states, united kindom’;
$query = $db->query("select value from countries where value like ‘$querystring%’ limit 10");
if($query) {
// while there are results loop through them - fetching an object (i like php5 btw!).
while ($result = $query ->fetch_object()) {
// format the results, im using <li> for the list, you can change it.
// the onclick function fills the textbox with the result.
echo ‘<li onclick="fill(’‘.$result->value.’‘);">’.$result->value.‘</li>’;
}
} else {
echo ‘error: there was a problem with the query.’;
}
} else {
// dont do anything.
} // there is a querystring.
} else {
echo ‘there should be no direct access to this script!’;
}
}
?>
php代碼解釋:
鑒于代碼中我已經加了很多注釋,在這里我就不再說的很詳細了。
一般情況下,需要接收這個 ‘querystring’ 然后在其最后使用通配符產生一個查詢語句。
這意味著在這種情況下,每次敲進去一個字符都需要產生一個查詢語句,如果一直都這樣做的話,恐怕mysql會受不了。但是為了盡量的簡化這個過程,這種做法對一個規模較小的應用應該沒什么問題。
這段php代碼你需要在自己的系統中稍作修改,比如你需要更新‘$query’到你自己的數據庫,需要看在哪里放你數據庫表的列名等等。
css樣式:
我使用的是css3,天哪,它真的很好用,雖然在firefox 或者safari瀏覽器上會有功能限制。
<style type="text/css">
.suggestionsbox {
position: relative;
left: 30px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #212427;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}
.suggestionlist {
margin: 0px;
padding: 0px;
}
.suggestionlist li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionlist li:hover {
background-color: #659cd8;
}
</style>
css代碼都很標準,沒什么需要特別指出的。
|||
主文件html:
這是主文件的部分html代碼,你需要添加的就是一個輸入框,并且把 ‘onkeyup’ 函數設置為lookup(this.value)。另外,我建議你不要修改它的id,如果你不想修改上面的javascript代碼的話。
截圖:
我想你應該會想要看看最后的效果是什么樣子,ok。
還有,
最后就是有用的鏈接了,我想你應該期待很久了。
演示: auto complete demo
源文件:autocomplete source zip
新聞熱點
疑難解答