jQueryUI之Autocomplete

Autocomplete使用場景javascript

場景1、官網相似百度谷歌搜索,像Bootstrap-3-Typeahead插件等,若用戶對下拉信息陌生的狀況下,我的感受對於ERP系統場景一不是很好用css

場景2、點擊文本框就能實現查詢全部數據,若沒有數據,tip提示html

如下以場景二做爲案例:java

實現代碼以下jquery

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BootStrap-tooltip+jqueryUI+Autocomplate</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/jquery-ui.min.css">
    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/jquery-ui.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</head>
<body>    
    <div style="margin: 50px 50px">
        <label for="product_search">Product Search: </label>
        <input id="product_search" type="text" data-toggle="tooltip" data-placement="right" >
    </div>
    <script type="text/javascript">
        $(function(){
            var personList = [{
                id : "1",
                name: "Aragorn",},
            {
                id : "2",
                name: "GatesBill",
            },{
                id : "3",
                name: "HlloKitty",
            }];
            var $chooseSearch = $("#product_search");
            $chooseSearch.autocomplete({
                minLength: 0,
                change: function(event, ui) {
                    if (ui.item == null) {
                        var $this = $(this);
                            $this.val("");
                    }
                },
                select: function(event, ui) {
                    var $this = $(this);
                        $this.val(ui.item.name).trigger('change');
                }
            }).off("click").click(function() {
                var $that = $(this);
                 if (personList && personList.length > 0) {
                        for (var i = 0; i < personList.length; i++) {
                            personList[i].value = personList[i].name;
                        }
                        $that.autocomplete('option', 'source', personList);
                        $that.autocomplete('search', '');
                    } else {
                        //tips提示
                        setTimeout(function(){
                            $that.attr("data-original-title","沒有記錄!");
                            $that.tooltip("show");
                        },150);  
                    }
            });
        })
    </script>
</body>
</html>
相關文章
相關標籤/搜索