自动完成文本区域问题

发布于 2024-11-01 20:34:56 字数 1951 浏览 0 评论 0原文

这是我的 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 e r s 哦 n

代码中的哪一部分导致了这个问题?

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

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?

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

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

发布评论

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

评论(1

樱花落人离去 2024-11-08 20:34:56

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

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

尝试

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

Old question, but your split is the culprit

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

try

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

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