【问题标题】:Autocomplete with multiple keywords使用多个关键字自动完成
【发布时间】:2013-05-18 17:25:33
【问题描述】:

我不确定它是否可能,但我想这样做,以便 jquery-ui 自动完成功能与多个关键字一起工作以获得相同的结果。

这是一个example,但它相当旧,我似乎无法让它工作,即使使用较旧的 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>

【问题讨论】:

  • “多关键字”是什么意思?你能举一些例子吗?
  • 您的链接示例运行良好,不是吗?
  • 我在互联网上找到了那个例子,但不能让它在我的代码和多关键字中工作,如果你看这个例子你可以看到当你输入“win”和“day ” 你会“赢得胜利”
  • 您的 jsFiddle 工作正常。我没有收到错误。发布您的完整代码和您遇到的错误。

标签: jquery jquery-ui autocomplete


【解决方案1】:

如果我正确理解你的问题,你想显示匹配同一句子的多个单词的列表。

例如点击here

 <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>

【讨论】:

  • 看起来这也会在您输入第二个单词时关闭,在我的代码和示例中尝试过,但不行 =(
  • 你能具体解释一下你想要什么吗?
  • 如果你正常一起输入“win”和“day”你不会得到结果,但我还是想得到“win the day”的结果
  • K 我现在明白了。我会尽快回复。
  • 你的例子 (jsfiddle.net/Q4jy9/1) 对我来说很好。我没有收到任何错误。我已经更新了我的答案(你的小提琴示例)。一探究竟。仍然没有得到它的工作发布你的完整代码。所以我们可以帮助你。
【解决方案2】:

我正在从数据库中获取城市区域名称并使用自动完成显示它。

    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'
        );
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2019-05-18
    相关资源
    最近更新 更多