多关键词自动完成

4
我不确定是否可能,但我希望使jquery-ui自动完成在同一结果中使用多个关键字。

这是一个示例,但它相当陈旧,即使使用较旧的jquery文件,我也无法让它正常工作。我对jquery和javascript不是很熟悉,但我可以编辑现有内容。

这是我目前拥有的(没有任何调整多关键字):

    <script type="text/javascript">
    $(document).ready(function() {
        NewAuto();
    });

    function NewAuto() {
        var products = [
        <?php foreach($search__1 as $search) {
        echo "{value: '". $search['product_name'] ."'}, ";}?>
        ];
        $("#keyword").autocomplete({
            source: function(requestObj, responseFunc) {
                var matchArry = products.slice(); // Copy the array
                var srchTerms = $.trim(requestObj.term).split(/\s+/);
                // For each search term, remove non-matches.
                $.each(srchTerms, function(J, term) {
                    var regX = new RegExp(term, "i");
                    matchArry = $.map(matchArry, function(item) {
                        return regX.test(item) ? item : null;
                    });
                });
                // Return the match results.
                responseFunc(matchArry);
            },
            open: function(event, ui) {
                // This function provides no hooks to the results list, so we have to trust the selector, for now.
                var resultsList = $("ul.ui-autocomplete > li.ui-menu-item > a");
                var srchTerm = $.trim($("#keyword").val()).split(/\s+/).join('|');
                // Loop through the results list and highlight the terms.
                resultsList.each(function() {
                    var jThis = $(this);
                    var regX = new RegExp('(' + srchTerm + ')', "ig");
                    var oldTxt = jThis.text();
                    jThis.html(oldTxt.replace(regX, '<span class="srchHilite">$1</span>'));
                });
            }
        });
    }

</script>

“多关键字”是什么意思?你能举几个例子吗? - asifrc
你的链接示例运行良好,不是吗? - A. Wolff
我在网上找到了这个例子,但是无法在我的代码中使其工作,并且有多个关键字。如果你看一下这个例子,你会发现当你输入“win”和“day”时,你会得到“赢得这一天”。 - Akano
你的jsFiddle工作正常。我没有收到任何错误。请发布你的完整代码和错误信息。 - Lakshmana Kumar
2个回答

3

如果我理解你的问题正确,你想展示符合同一个句子中多个单词的列表。

点击这里查看示例。

 <html>
 <head>
    <title>Testing</title>
    <link href="css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .srchHilite { background: yellow; }
    </style>
    <script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="scripts/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function() {
            NewAuto();
        });

        function NewAuto() {
            var availableTags = ["win the day", "win the heart of", "win the heart of someone"];
            alert(availableTags);  // alert = win the day,win the heart of,win the heart of someone
            $("#tags").autocomplete({
                source: function(requestObj, responseFunc) {
                    var matchArry = availableTags.slice(); // Copy the array
                    var srchTerms = $.trim(requestObj.term).split(/\s+/);
                    // For each search term, remove non-matches.
                    $.each(srchTerms, function(J, term) {
                        var regX = new RegExp(term, "i");
                        matchArry = $.map(matchArry, function(item) {
                            return regX.test(item) ? item : null;
                        });
                    });
                    // Return the match results.
                    responseFunc(matchArry);
                },
                open: function(event, ui) {
                    // This function provides no hooks to the results list, so we have to trust the selector, for now.
                    var resultsList = $("ul.ui-autocomplete > li.ui-menu-item > a");
                    var srchTerm = $.trim($("#tags").val()).split(/\s+/).join('|');
                    // Loop through the results list and highlight the terms.
                    resultsList.each(function() {
                        var jThis = $(this);
                        var regX = new RegExp('(' + srchTerm + ')', "ig");
                        var oldTxt = jThis.text();
                        jThis.html(oldTxt.replace(regX, '<span class="srchHilite">$1</span>'));
                    });
                }
            });
        }

    </script>
</head>
<body>
    <div>
        <label for="tags">
            Multi-word search:
        </label>
        <input type="text" id="tags" />
    </div>
</body>
</html>

1
如果您通常输入“win”和“day”,您将不会得到结果,但我仍然希望获得“赢得这一天”的结果。 - Akano
你的例子(http://jsfiddle.net/Q4jy9/1/)对我来说运行良好。我没有收到任何错误。我已经更新了我的答案(你的fiddle例子)。看看吧。如果仍然无法正常工作,请发布您的完整代码,以便我们可以帮助您。 - Lakshmana Kumar
我正在发布另一个我使用的答案。 - Lakshmana Kumar
我认为您没有创建支持自动完成的正确数组,因此它才无法正常工作。 - Lakshmana Kumar
谢谢,警报显示它们是对象,我删除了值和{}(我以前用过它,但现在不再使用),现在它可以工作了,再次感谢,我已经在这上面工作了几周。 - Akano
显示剩余6条评论

0

我从数据库中获取城市地区名称,并使用自动完成功能进行显示。

    function GetLocalityList() {
        var LocalityArray = [];
        $.post("MvcLayer/Index/GetLocalityList",
        {
            CityID: $('#sltCity').val()
        },
        function(data) {
            // My sql query will be like this select LocalityID, CityID, LocalityName from tablename where CityID = 20
            // Here (data) is array format. Like this
            // [{"LocalityID":9397,"CityID":55,"LocalityName":"Adugodi"},{"LocalityID":9398,"CityID":55,"LocalityName":"Aga Abbas Ali Road"},{"LocalityID":9399,"CityID":55,"LocalityName":"Agaram"},{"LocalityID":9400,"CityID":55,"LocalityName":"Agrahara Dasara Halli"},{"LocalityID":9401,"CityID":55,"LocalityName":"Agrahara Dasarahalli"},{"LocalityID":9402,"CityID":55,"LocalityName":"Airport Exit Road"},{"LocalityID":9403,"CityID":55,"LocalityName":"Horamavu"},{"LocalityID":9404,"CityID":55,"LocalityName":"Hosakere Halli"},{"LocalityID":9405,"CityID":55,"LocalityName":"Hennur"},{"LocalityID":9406,"CityID":55,"LocalityName":"Hesaraghatta"},{"LocalityID":9407,"CityID":55,"LocalityName":"HKP Road"},{"LocalityID":9408,"CityID":55,"LocalityName":"HMT Layout"},{"LocalityID":9409,"CityID":55,"LocalityName":"Hongasandra"},{"LocalityID":9410,"CityID":55,"LocalityName":"Hoody"},{"LocalityID":9411,"CityID":55,"LocalityName":"Hayes Road"}    ]

            $.each(data, function(key, value) {
                LocalityArray[key] = value.LocalityName;
            });
            $("#txtLocality" + SelectedTab).autocomplete({
                minLength: 1,
                source: function(req, responseFn) {
                    //  \\b show each match letter in each word of list
                    //  ^ show each match letter in whole word of list
                    var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(req.term), "i");
                    var a = $.grep(LocalityArray, function(item, index) {
                        return matcher.test(item);
                    });
                    responseFn(a);
                }
            });
        },
        'json'
        );
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接