3

我正在实现 jquery 自动完成,这里是代码,

<script>
$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Scheme"
    ];
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    $( "#tags" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            minLength: 0,
            source: function( request, response ) {
                // delegate back to autocomplete, but extract the last term
                response( $.ui.autocomplete.filter(
                    availableTags, extractLast( request.term ) ) );
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            }
        });
});
</script>

这是 HTML 代码:

 <label for="tags">Tag programming languages: </label>

 <textarea rows="3" cols="50" id="tags"></textarea>

现在的问题是,当我输入“a”或“s”时,我会从自动完成中获取相关数据。我想以这样一种方式实现,当我按下“空格键”时,我需要获取所有数据。我该怎么做。请帮助...

4

1 回答 1

0

如果你可以定义一个函数 extractAll 你可以做如下

if(request.term == " ")
    response( $.ui.autocomplete.filter(
                        availableTags, extractAll() ) );
    else
    response( $.ui.autocomplete.filter(
                        availableTags, extractLast( request.term ) ) );
于 2012-12-13T07:18:51.747 回答