jquery+php查詢mysql實現自動完成功能

來源:互聯網
上載者:User

<script src="jquery-1.2.1.pack.網頁特效" type="text/網頁特效"></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>

jquery+php查詢mysql實現自動完成功能,寫這個教程,因為曾經見到的大部分關於自動完成的應用程式都只是給你一個程式源碼包,然後告訴你怎麼使用,而不是告訴你它是如何工作的以及為什麼這樣做。而知道這些可以讓你對這個外掛程式可以進一步的按自己的需求定製(關於這一點我在我的blog裡寫過不少關於其他應用的文章)。

方法二

json

$(function(){
var text = $(".input").text();
settimeout(function(){
if($(".input").text() != text )
{
text = $(".input").text();
$.ajax({
type:"get",
url:"xxxx", //放你的php url
data:{text:text },   //你php接受的資料  名字叫text
datatype:"json",     //傳遞迴來時候用json方式
error:function()
{
alert("ajax載入失敗");
},
success: function(json)
     {//json為返回的資料,在這裡做資料繫結
   var ottery = json.data;
   var $xiala = $('<div class="xiala><ul id="datas" ><li id="row" ><li></ul></div>');       //><li id="row" ><li>是存放資料的模板
     for(var i=1;i<ottery.length;i++)
        {
        var row = $("#row").clone();
        row.html(ottery[i].sssj);
        row.attr("id","ready");//改變綁定好資料的行的id
        row.appendto("#datas");//添加到模板的容器中     
        }
       $("#row").hide();
       $xiala.appendto("body"); 
    })
  }
},10);
})

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.