php+ajax即時擷取下拉資料程式碼

來源:互聯網
上載者:User

你點擊需要的資料後,這個資料寫如到當前輸入框。
並在後面添加逗號隔開,繼續輸入的時候,幕後處理繼續輸出資料以供選擇。

下面我們來看執行個體,html代碼

 代碼如下 複製代碼

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
ajax即時擷取下拉資料</pre>
<style><!--
.input_s{ position:relative}
.input_s ul{ list-style:none; margin:0; padding:0; width:200px; border:1px solid #ccc; border-bottom:none}
.input_s ul li{ border-bottom:1px solid #ccc}
.input_s ul li:hover{ color:#fff; background:#000}
--></style>
<pre></pre>
<div class="input_s"><input class="tla" id="tla" style="width: 500px;" type="text" name="tla" />
</div>
<pre>
<script type="text/javascript">// <![CDATA[
var funjieliu = function(fn, delay){//函數節流  add by shanmao 2013 - 1 - 18
    var timer = null;
    return function(){
        var context = this, args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function(){
            fn.apply(context, args);
        }, delay);
    };
 };
document.getElementById("tla").onkeyup=funjieliu(function(){//鍵盤按下的時候
    var tla = $("#tla").val();
    if(tla){
        $.post("/cityosweb/default.php/shanmao/input_xiala",{tla:tla},function(data){
            if(data.status==1){
                $(".inul").html("");
                $.each(data.data,function(index,val){
                    $(".inul").append("
    <li>"+val.username+"</li>
 
");
                    });
            }
            },"json");
        }
    },500);
$(function(){
    $(".inul li").live("click",function(){
        var thval = $(this).html();
        var tla = $("#tla").val();
        var regexp = new RegExp(",");
        if(regexp.test(tla)){//如果input有值(",")則加上input裡面的值
        $("#tla").val(tla+thval+",");
            }else{
        $("#tla").val(thval+",");
                }
        $(".inul").html("");
        $("#tla").focus();
        });
    });
// ]]></script>

php代碼如上

 代碼如下 複製代碼

function input_xiala(){
    $input = new input();
    $tval = $input->post('tla');
    $u = M('User');
    if(strpos($tval,",")){//檢查是否帶有","
        $val = explode(",",$tval);//拆分成數組
        $tval = end($val);//數組的最後一個值
        }
    $re = $u->field('username,email')->where("username like '$tval%'")->limit(10)->select();
    $this->ajaxReturn($re,'success',1);
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.