关于jquery ui auto complete自动填充

好久没有写博客了,前几天想写突然发现前段时间的东西没有记录下来。于是今天完成了一个小功能记录下来

实现如同百度的输入框联想:

前端展示界面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>注册公司自动匹配</title>
<link href="/auto/jquery-ui.css" />
<!-- 引入jQuery的js文件 -->
<script type="text/javascript" src="/auto/jquery-1.9.1.js" ></script>
<!-- 引入jQuery UI的js文件 -->
<script type="text/javascript" src="/auto/jquery-ui.js" ></script>
</head>
<body>
    <div style="margin:20px 50px;">
        <input id="txtSearchKey" name = "txtSearchKey" type="text" class="text ui-widget-content ui-corner-all" style="width:250px; height:30px;" />
        <br><input type = "text" name ="companyid" id = "companyid"/>
        <input id="btnSubmit" type="button" class="ui-button ui-widget ui-state-default ui-corner-all" value="搜索" />
    </div>
    <script type="text/javascript">
        $(function() {
            $("#txtSearchKey").autocomplete(
            {
                source: function(request, response) {
                    $.ajax({
                        url: "/chuqq/index.php/company/auto",
                        dataType: "json",
                        data: {
                            top: 10,
                            key: request.term
                        },
                        success: function(data) {
                            response($.map(data.companylist, function(item) {
                                return { label: item.name, value: item.name ,id: item.id }
                            }));
                        }
                    });
                },
          select: function(event, ui) {
                    //提交搜索...
                },
                minLength: 2, 
                autoFocus: false, 
                delay: 500 
            });
        });
    </script>
</body>
</html>

后台处理程序:完成

        //自动匹配公司  
        public function auto(){
                 $key = isset($_GET['key']) ? $_GET['key'] : '';
                 $sql = "select * from company where name like '%$key%'";
                 $companys = $this->company_model->getQuery($sql);
                 $res = array("companylist"=>$companys);
                 echo json_encode($res);
        }

 

转载于:https://my.oschina.net/passer007/blog/675929

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值