开发者

Autocomplete textarea problem

开发者 https://www.devze.com 2023-02-25 12:43 出处:网络
Here is my autocomplete script for textarea $(function() { function split( val ) { return val.split( /,\\s*/ );

Here is my autocomplete script for textarea

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

    $( "#readers" )
        // 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({
            source: function( request, response ) {
                $.get( "test.html", {
                    term: extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: func开发者_开发技巧tion( 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( "\n " );
                return false;
            }
        });
});

The very simple test.html controller method:

  @RequestMapping(value = "test.html", method = RequestMethod.GET)
    public @ResponseBody String personquery(HttpServletRequest request) {


        String personList = "person";
return personList;

}

Autocomplete works now, but the problem is, that suggestion list returns only one character for user to choose, in other words a list like p e r s o n

Which part in the code causes this?


Old question, but your split is the culprit

http://jsfiddle.net/mplungjan/XVccx/

try

return val.split( /[, ]/ );

0

精彩评论

暂无评论...
验证码 换一张
取 消