【问题标题】:jQuery UI Autocomplete dividing terms into separate characters, not wordsjQuery UI 自动完成将术语划分为单独的字符,而不是单词
【发布时间】:2011-02-17 02:43:03
【问题描述】:

我正在编写一个 ASP.NET MVC 站点,我正在尝试使用 jQuery UI 自动完成功能实现一个自动完成功能

这是我的页面的设置方式:

我有一个 ID 为 TagInput 的文本框。接下来,我有以下内联 Javascript 代码:

$().ready(function () {
            bindAutoTagComplete('#TagInput');
        });

除了引用该页面上的 jQuery 和 jQuery UI 库之外,我还引用了这个外部 Javascript 代码:

function bindAutoTagComplete(item, otherRootDomain)
{
var url = (otherRootDomain || "") + "/tags/ajaxList";
        function split( val ) {
            return val.split(' ');
        }
        function extractLast( term ) {
            return split( term ).pop();
        }

        $(item).autocomplete({
            source: function( request, response ) {
                $.getJSON(url, {
                    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( " " );
                return false;
            }
        });
    }

一切正常,除了带有标签名称的 AJAX 结果被分成单个字符,而不是单词。 例如,如果我有两个标签“foo”和“bar”被返回,弹出列表的自动完成列表:

  • f
  • o
  • o
  • b
  • 一个
  • r

代替:

  • 酒吧

我已经调试了代码,但没有找到 是什么导致了这种不正确的划分。有什么想法吗? 提前致谢。

更新:这是服务器当前在该 AJAX 请求中返回的示例:

"foo bar some-other-tag"

【问题讨论】:

  • 您能否提供从服务器返回的 json 结果示例?

标签: jquery jquery-ui autocomplete jquery-autocomplete jquery-ui-autocomplete


【解决方案1】:

问题很可能是split 函数,它看起来像是一些自定义拆分函数。很难说,因为这里没有所有相关的代码。

【讨论】:

    【解决方案2】:

    感谢您的提示,伙计们!我发现了问题;事实上,有两个:

    1. 我的应用程序以格式不正确返回 AJAX 结果。根据 jQuery UI Autocomplete 文档,我已将其更改为正确的格式。
    2. 我的 AJAX 调用也将标签名称作为错误的参数发送。

    现在可以了!

    【讨论】:

      猜你喜欢
      • 2011-03-30
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2011-02-06
      • 2013-01-18
      • 1970-01-01
      • 2011-08-16
      相关资源
      最近更新 更多