jQuery UI 自动完成将术语划分为单独的字符,而不是单词

发布于 2024-10-17 00:42:28 字数 2098 浏览 2 评论 0原文

我正在编写一个 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
  • a
  • r

而不是:

  • foo
  • bar

I'已经调试了代码,但还没有找到导致这种错误划分的原因。有什么想法吗? 提前致谢。

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

“foo bar 其他标签”

I'm writing an ASP.NET MVC site where I'm trying to implement an autocomplete function using the jQuery UI autocomplete feature.

Here's how my page is setup:

I have a textbox with id TagInput. Next, I have the following inline Javascript code:

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

In addition to referencing the jQuery and jQuery UI libraries on that page, I also reference this external Javascript code:

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

Everything works fine, except that the AJAX result with tag names is divided into single characters, not words. For example, if I have two tags "foo" and "bar" that are returned, the autocomplete list that pops up lists:

  • f
  • o
  • o
  • b
  • a
  • r

instead of:

  • foo
  • bar

I've debugged the code, but haven't found what is causing this incorrect division. Any ideas? Thanks in advance.

UPDATE: Here's a sample of what's currently being returned by the server in that AJAX request:

"foo bar some-other-tag"

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

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

发布评论

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

评论(2

沩ん囻菔务 2024-10-24 00:42:28

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

The problem is most probably the split function which looks like it's some custom split function. It's hard to say since there's not all relevant code here.

心清如水 2024-10-24 00:42:28

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

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

现在可以了!

Thanks for your tips, guys! I found the problem; in fact, there were two:

  1. My application was returning the AJAX result in incorrect format. Based on the jQuery UI Autocomplete documentation, I have changed it to the correct format.
  2. My AJAX call was sending the tag name as the wrong parameter, too.

It works now!

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