zoukankan      html  css  js  c++  java
  • 百度搜索框自动补全

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title></title>
        <link rel="stylesheet" href="/jquery-weui-build/dist/lib/weui.min.css">
        <link rel="stylesheet" href="/jquery-weui-build/dist/css/jquery-weui.css">
        <link rel="stylesheet" href="/jquery-weui-build/demos/css/demos.css">
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="/static/jquery-weui-build/dist/lib/fastclick.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    
        <script>
            $(function () {
                FastClick.attach(document.body);
            });
        </script>
        <script src="/jquery-weui-build/dist/js/jquery-weui.js"></script>
    </head>
    <body>
    
    <div class="ui-widget">
    
    
        <div class="weui-search-bar" id="searchBar">
            <form class="weui-search-bar__form" action="#">
                <div class="weui-search-bar__box">
                    <i class="weui-icon-search"></i>
                    <input type="search" class="weui-search-bar__input" id="tags" placeholder="搜索" required=""
                           onkeyup="catch_keyword(this.value)">
                    <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
                </div>
                <label class="weui-search-bar__label" id="searchText"
                       style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);">
                    <i class="weui-icon-search"></i>
                    <span>搜索</span>
                </label>
            </form>
            <a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
        </div>
    </div>
    
    <script>
        var availableTags = [];//数据源
    
        //先初始化自动补全功能
        $("#tags").autocomplete({
            source: availableTags //数据源
        });
    
        //去掉字符串中任意位置的空格
        function Trim(str, is_global) {
            var result;
            result = str.replace(/(^s+)|(s+$)/g, "");
            if (is_global.toLowerCase() == "g") {
                result = result.replace(/s/g, "");
            }
            return result;
        }
    
        //判断字符串是否全是中文
        function isChn(str) {
            var reg = /^[u4E00-u9FA5]+$/;
            if (!reg.test(str)) {
                return false;
            } else {
                return true;
            }
        }
    
        //捕捉键入的关键字
        function catch_keyword(word = null) {
    
            if (isChn(Trim(word, 'g'))) {
                get_source(word);
                $("#tags").autocomplete({
                    source: availableTags //数据源
                });
    
            }
        }
    
        //请求后端获取数据源
        function get_source(word = null) {
            var url = "<?php echo base_url('admin/Demo/source');?>?keyword=" + word;
            $.get({
                type: 'GET',
                url: url,
                async: false,//改为同步
                dataType: 'json',
                success: function (response) {
                    console.log('1');
                    availableTags = response;
                }
            });
        }
    
    </script>
    </body>
    </html>

    转载 https://www.jianshu.com/p/18047be090f4

    <script>
        var availableTags = [];//数据源
    
        //先初始化自动补全功能
        $("#tags").autocomplete({
            source: availableTags //数据源
        });
    
        //去掉字符串中任意位置的空格
        function Trim(str, is_global) {
            var result;
            result = str.replace(/(^s+)|(s+$)/g, "");
            if (is_global.toLowerCase() == "g") {
                result = result.replace(/s/g, "");
            }
            return result;
        }
    
        //判断字符串是否全是中文
        function isChn(str) {
            var reg = /^[u4E00-u9FA5]+$/;
            if (!reg.test(str)) {
                return false;
            } else {
                return true;
            }
        }
    
        //捕捉键入的关键字
        function catch_keyword(word = null) {
    
            if (isChn(Trim(word, 'g'))) {
                get_source(word);
                $("#tags").autocomplete({
                    source: availableTags //数据源
                });
    
            }
        }
    
        //请求后端获取数据源
        function get_source(word = null) {
            var url = "<?php echo base_url('admin/Demo/source');?>?keyword=" + word;
            $.get({
                type: 'GET',
                url: url,
                async: false,//改为同步
                dataType: 'json',
                success: function (response) {
                    console.log('1');
                    availableTags = response;
                }
            });
        }
    
    </script>
  • 相关阅读:
    常见排序算法-----堆排序
    深度优先搜索和广度优先搜索
    剑指offer整理-------二维数组查找
    常见排序算法-----希尔排序
    log4j日志不能输出到文件
    常见排序算法-----直接插入排序
    从零开始学习springBoot(使用thymeleaf和freemarker模板)
    从零开始学习springBoot(Spring Boot使用Druid和监控配置)
    从零开始学习springBoot(定时任务)
    从零开始学习springBoot(默认静态资源和自定义资源映射)
  • 原文地址:https://www.cnblogs.com/isuansuan/p/10689177.html
Copyright © 2011-2022 走看看