如何触发多远程jquery自动完成UI的自动搜索选项

发布于 2024-11-09 08:56:09 字数 2164 浏览 0 评论 0原文

我正在使用这个自动完成库: http://jqueryui.com/demos/autocomplete/#multiple -remote

我想知道是否有人知道如何触发自动搜索而无需在文本框中输入任何内容。即如果我们想在按钮单击事件上显示列表。 “搜索”方法似乎就是这样做的。但我无法让它工作。

请注意,自动建议的数据来自网络服务。如果用户在文本框中键入内容,建议会相应更改。即键入的数据进入服务并带回建议。 输入的形式为“名称、位置”。因此,对于输入的不同部分,会显示不同类型的建议。

代码:

UI:

<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds" size="50" />
</div>

脚本:

<script>
    $(function() {
        function split( val ) {
            return val.split( /,\s*/ );
        }
        function extractLast( term ) {
            return split( term ).pop();
        }

        $( "#birds" )               
            .bind( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                        $( this ).data( "autocomplete" ).menu.active ) {
                    event.preventDefault();
                }
            })
            .autocomplete({
                source: function( request, response ) {
                    $.getJSON( "search.php", {
                        term: extractLast( request.term )
                    }, response );
                },
                search: function() {
                    var term = extractLast( this.value );
                    if ( term.length < 2 ) {
                        return false;
                    }
                },
                focus: function() {
                    return false;
                },
                select: function( event, ui ) {
                    var terms = split( this.value );
                    terms.pop();
                    terms.push( ui.item.value );
                    terms.push( "" );
                    this.value = terms.join( ", " );
                    return false;
                }
            });
    });
    </script>

如果我输入任何内容,它就会向 search.php 发送请求。 我尝试将逻辑“term.length < 2”更改为“term.length <= 0”。这可行,但我必须按空格键。然后在文本框中放置一个空白区域,并将请求发送到服务器。但我不想在那里输入任何内容。 希望有帮助。

I am using this auto complete library: http://jqueryui.com/demos/autocomplete/#multiple-remote

I was wondering if anyone knows how trigger the auto search without typing anything in the text box. i.e. if we want to display the list on a button click event.
the "search" method seems to be doing that. But I cant get it working.

Please note that the data of the auto suggest comes from a web service. If user types something in the text box the suggestion changes accordingly. i.e. the typed data goes to the service and brings back the suggestion.
The input is of "Name, Location" form. As a result for different parts of the input different types of suggestions are displayed.

Codes:

UI:

<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds" size="50" />
</div>

Script:

<script>
    $(function() {
        function split( val ) {
            return val.split( /,\s*/ );
        }
        function extractLast( term ) {
            return split( term ).pop();
        }

        $( "#birds" )               
            .bind( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                        $( this ).data( "autocomplete" ).menu.active ) {
                    event.preventDefault();
                }
            })
            .autocomplete({
                source: function( request, response ) {
                    $.getJSON( "search.php", {
                        term: extractLast( request.term )
                    }, response );
                },
                search: function() {
                    var term = extractLast( this.value );
                    if ( term.length < 2 ) {
                        return false;
                    }
                },
                focus: function() {
                    return false;
                },
                select: function( event, ui ) {
                    var terms = split( this.value );
                    terms.pop();
                    terms.push( ui.item.value );
                    terms.push( "" );
                    this.value = terms.join( ", " );
                    return false;
                }
            });
    });
    </script>

If I type anything it sends a request to search.php.
I tried changing the logic "term.length < 2" to "term.length <= 0". This works but i have to press the space bar. Then an empty space is placed in the text box and is sends the request to the server. But I don't want to type anything in there.
Hope that helps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金兰素衣 2024-11-16 08:56:09

如果您想在单击按钮时触发搜索,则必须调用搜索方法。如果您想显示所有选项,请调用搜索并将值设置为空字符串,并将自动完成小部件设置为接受 minLength: 0。

因此,在代码中:

UI

<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds" size="50" />
    <input type="button" id="search-trigger" value="Trigger" />
</div>

脚本

<script type="text/javascript">
$(function() {
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    // Button listener that triggs the search event
    $("#search-trigger").click(function(){
        var searchTerm = $( "#birds" ).val();
        $( "#birds" ).autocomplete( "search" , searchTerm);
    });


    $( "#birds" )               
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: ["lorem", "ipsum", "dolor"], 
                /* Commented this out to use dummy data above
                            function( request, response ) {
                $.getJSON( "search.php", {
                    term: extractLast( request.term )
                }, response );
            },*/
            search: function() {
                var term = extractLast( this.value );

                          /* What is this check for?
                if ( term.length < 2 ) {
                    return false;
                }*/
            },
            focus: function() {
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                terms.pop();
                terms.push( ui.item.value );
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            },
            //Added for "show all" support
            minLength: 0
        });
});
</script>

这是您正在寻找的行为吗?

参考:http://jqueryui.com/demos/autocomplete/#method-search

If you want to trigger the search when a button is clicked, then you have to call the search method. If you want to show all options, call search with the value set to empty string and set the autocomplete widget to accept minLength: 0.

So in code:

UI

<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds" size="50" />
    <input type="button" id="search-trigger" value="Trigger" />
</div>

Script

<script type="text/javascript">
$(function() {
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    // Button listener that triggs the search event
    $("#search-trigger").click(function(){
        var searchTerm = $( "#birds" ).val();
        $( "#birds" ).autocomplete( "search" , searchTerm);
    });


    $( "#birds" )               
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: ["lorem", "ipsum", "dolor"], 
                /* Commented this out to use dummy data above
                            function( request, response ) {
                $.getJSON( "search.php", {
                    term: extractLast( request.term )
                }, response );
            },*/
            search: function() {
                var term = extractLast( this.value );

                          /* What is this check for?
                if ( term.length < 2 ) {
                    return false;
                }*/
            },
            focus: function() {
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                terms.pop();
                terms.push( ui.item.value );
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            },
            //Added for "show all" support
            minLength: 0
        });
});
</script>

Is this the behavior you are looking for?

Ref: http://jqueryui.com/demos/autocomplete/#method-search

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文