溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php+mysql+jquery如何實(shí)現(xiàn)檢索自動(dòng)補(bǔ)全提示功能

發(fā)布時(shí)間:2021-06-25 09:23:43 來源:億速云 閱讀:179 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹php+mysql+jquery如何實(shí)現(xiàn)檢索自動(dòng)補(bǔ)全提示功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

這段時(shí)間看了一些關(guān)于自動(dòng)補(bǔ)全提示方面的內(nèi)容,發(fā)現(xiàn)大部分實(shí)現(xiàn)過程都十分復(fù)雜。心想這應(yīng)該是一個(gè)比較簡單的功能啊,于是自己親自動(dòng)手來小試一下。思路很常規(guī),需要說明的是沒有為提示內(nèi)容綁定鍵盤事件,僅可用鼠標(biāo)操作。

html+jQuery內(nèi)容:

<html>
<head>
  <style type="text/css">
    #autoBox
    {
      margin: 0px;
      padding: 0px;
      border: 1px solid #CCCCCC;
      width: 200px;
    }
    #autoBox li
    {
      clear: both;
      background-color: white;
      color: black;
      position: relative;
      top: 0px;
      left: 0px;
      line-height: 25px;
      width:200px;
      text-align: left;
      overflow: hidden;
    }
    #autoBox li:hover
    {
      background-color: gray;
      color: yellow;
      cursor: pointer;
    }
  </style>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
  $(function(){
    $.ajaxSetup({cache:false}) ; //不緩存
    //以下代碼用于顯示檢索提示框
    $("#autoBox").hide(); //初始化時(shí)隱藏補(bǔ)全提示框
    $("#stuSearch").keyup(function(){ //輸入框中的keyup事件激活以下查詢行為
      $("#autoBox").html(""); //先清空補(bǔ)全提示框原有內(nèi)容
      if($("#stuSearch").val().length>0) // 如果輸入框不為空
      {
        $.ajax({ //后臺(tái)調(diào)用php文件進(jìn)行查詢
          type:"post",
          url:"phpFiles/stuSearch.php",
          dataType:"json",
          data:{keywords:$("#stuSearch").val()},
          success:function(feedbackdata)
            {
              $("#autoBox").show();// 顯示補(bǔ)全提示框
              for(i=0;i<feedbackdata.length;i++) //將結(jié)果添加到提示框中
              {
                $("#autoBox").append("<li>"+feedbackdata[i]['truename']+"</li>");
              }
              $("#autoBox li").on("click",function(){ //為這些新增的li綁定單擊事件,單擊后將其值賦到輸入框中
                $("#stuSearch").val($(this).text());
              })
              $("#autoBox").append("<li style='text-align:right'>關(guān)閉</li>");//在提示框的最后添加一個(gè)li用來關(guān)閉
              $("#autoBox li:last").on("click",function(){ // 添加單擊事件,單擊后隱藏提示框
                $("#autoBox").hide();
              })
            }
        });
      }
    })
  })
</script>
</head>
<body>
<input type="text" placeholder="輸入您想檢索的關(guān)鍵詞后回車。" id="stuSearch" name="stuSearch"/>
<ul id="autoBox">
</ul>
</body>
</html>

stuSearch.php代碼

<?php
  include '../phpFiles/connMysql.php';
  $keywords=$_POST['keywords'];
  $myrs=mysql_query("select * from users where truename like '".$keywords."%'");
  if($myrs)
  {
    while($row=mysql_fetch_array($myrs))
    {
      $temp[]=$row;
    }
    echo(json_encode($temp));
  }
?>

效果圖

輸入時(shí):

php+mysql+jquery如何實(shí)現(xiàn)檢索自動(dòng)補(bǔ)全提示功能

點(diǎn)擊某一項(xiàng)后:

php+mysql+jquery如何實(shí)現(xiàn)檢索自動(dòng)補(bǔ)全提示功能

以上是“php+mysql+jquery如何實(shí)現(xiàn)檢索自動(dòng)補(bǔ)全提示功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI