自动完成textarea问题

时间:2011-04-15 08:05:08

标签: jquery autocomplete textarea

这是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: 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( "\n " );
                return false;
            }
        });
});

非常简单的test.html控制器方法:

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


        String personList = "person";
return personList;

}

自动完成现在可以正常工作,但问题是,该建议列表只返回一个字符供用户选择,换句话说就像一个列表 p Ë [R 小号 Ø Ñ

代码中的哪一部分会导致这种情况?

1 个答案:

答案 0 :(得分:0)

老问题,但你的分裂是罪魁祸首

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

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