bigautocomplete实现联想输入,自动补全

bigautocomplete是一款Jquery插件。用它实现仿搜索引擎文本框自动补全插件功能很实用,使用也很简单,引入了插件之后写几行代码就可以实现,可以灵活设置。

效果图:


一、如何使用:

   引入jquery.bigautocomplete.js和jquery.bigautocomplete.css文件到你的页面中。

二、参数说明:

$("xxxxx").bigAutocomplete({data:[...],url:"",width:0,callback:{}})
 
 
参数说明
data(可选):
data:格式{data:[{title:null,result:{}},{title:null,result:{}}]}
url和data两个参数必须有一个,且只有一个生效,data优先。
url(可选):url为字符串,用来ajax后台获取数据,返回的数据格式为data参数一样。
width(可选):下拉框的宽度,默认使用输入框宽度。
callback(可选):选中行后按回车或单击时回调的函数,用于返回选中行的其他数据及做一些操作。

 

三、示例:

  1、本地数据:
html代码:
<input type="text" id="tt" value="" class="text" />
 
javascript代码:
$(function(){

    $("#tt").bigAutocomplete({
        width:543,
        data:[{title:"中国好声音",result:{ff:"qq"}},
        {title:"中国移动网上营业厅"},
        {title:"中国银行"},
        {title:"中国移动"},
        {title:"中国好声音第三期"},
        {title:"中国好声音 第一期"},
        {title:"中国电信网上营业厅"},
        {title:"中国工商银行"},
        {title:"中国好声音第二期"},
        {title:"中国地图"}],
        callback:function(data){
            alert(data.title);    
        }
    });

})

js中data里的result可以不写。

 

2、ajax请求:

html代码:
<input type="text" id="company" value="" class="text" />

javascript代码:


$(function(){
    $("#tt").bigAutocomplete({
        width:543,
        url:'http://localhost/test/suggestCom',
        callback:function(data){
            //alert(data.title);    
        }
    });
})

服务端返回数据格式:


{"data":[{"title":"\u5317\u4eac\u73b0\u4ee3"},{"title":"\u5317\u4eac\u57ce\u5efa\u96c6\u56e2\u6709\u9650\u8d23\u4efb\u516c\u53f8"},{"title":"\u5317\u4eac\u5efa\u5de5\u96c6\u56e2\u6709\u9650\u8d23\u4efb\u516c\u53f8"},{"title":"\u5317\u4eac\u9996\u90fd\u65c5\u6e38\u96c6\u56e2\u6709\u9650\u8d23\u4efb\u516c\u53f8"},{"title":"\u5317\u4eac\u533b\u836f\u96c6\u56e2\u6709\u9650\u8d23\u4efb\u516c\u53f8"},{"title":"\u5317\u4eac\u4e00\u8f7b\u63a7\u80a1\u6709\u9650\u8d23\u4efb\u516c\u53f8"},{"title":"\u5317\u4eac\u91d1\u9685\u96c6\u56e2\u6709\u9650\u8d23\u4efb\u516c\u53f8"},{"title":"\u5317\u4eac\u71d5\u4eac\u5564\u9152\u96c6\u56e2\u516c\u53f8"},{"title":"\u5317\u4eac\u5e02\u71c3\u6c14\u96c6\u56e2\u6709\u9650\u8d23\u4efb\u516c\u53f8"},{"title":"\u5317\u4eac\u4f4f\u603b\u96c6\u56e2\u6709\u9650\u8d23\u4efb\u516c\u53f8"}]}

服务端的代码:(以ThinkPHP示例)


public function suggestCom(){
        $wd = $_POST['keyword'];
        $keywords = $wd;
    
        $company_model = M('Company');
    
        $res = $company_model->where("name like '%".$keywords."%' or abbr like '%".$keywords."%' ")->limit(10)->select();
        foreach($res as $v){
            $suggestions[]= array('title' => $v['name']);
        }
    
        echo json_encode(array('data' => $suggestions));
    }
默认是POST过来的数据,名称是keyword,返回数据是和本地data一致的。

bigautocomplete是一个自动补全插件,由国人开发,支持中文。它可以实现联想输入自动补全的功能,可以应用于各种网站和应用程序中。以下是一个使用bigautocomplete的例子: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>bigautocomplete Demo</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdn.bootcss.com/bigautocomplete/1.0.0/bigautocomplete.min.css"> </head> <body> <div class="container"> <h1>bigautocomplete Demo</h1> <input type="text" id="autocomplete" class="form-control" placeholder="请输入..."> </div> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bigautocomplete/1.0.0/bigautocomplete.min.js"></script> <script> $(function() { var data = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']; $('#autocomplete').bigAutocomplete({ data: data, callback: function(data) { console.log(data); } }); }); </script> </body> </html> ``` 在这个例子中,我们引入了bigautocomplete的CSS和JavaScript文件,并在页面中添加了一个文本框。我们使用jQuery选择器选中这个文本框,并调用bigAutocomplete()方法来初始化bigautocomplete插件。我们传递了一个包含一些水果名称的数组作为数据源,并定义了一个回调函数来处理用户选择的结果。当用户在文本框中输入字符时,bigautocomplete会自动显示匹配的结果,用户可以通过键盘或鼠标选择一个结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值