在 JQuery 自动完成中使用当前选择器

发布于 2024-09-10 10:33:15 字数 623 浏览 2 评论 0原文

我正在使用 JQuery UI 的自动完成小部件,并且希望能够访问我将自动完成附加到的当前选择器。我找到了一种访问选择器的方法,使用这里的上一个问题,但是源函数被错误地调用。

我当前的代码如下所示

$("input[id$=artist]").each(function() {
    e1 = $(this);
    curID = $(this).parent('td').parent('tr').attr('id');       
    e1.autocomplete({
        minLength: 3,
        source: function(request, response) { 
            findSuggestions(request, response, 'artist', artist_cache, curID);
        }
    });
});

如您所见,我获取当前选择器并将其放入 e1 中。有多个行具有给定的“艺术家”输入,我希望能够知道 findSuggestions 方法中每一行的 ID,但是当调用该方法时,每一行都会被赋予相同的 ID (这是引用最后一行。

知道为什么会发生这种情况吗?

谢谢!

I'm using JQuery UI's autocomplete widget and would like to have access to the current selector which I'm attaching the autocomplete to. I have found a way to access the selector, using a previous question here, but the source function is called with incorrectly.

My current code looks like this

$("input[id$=artist]").each(function() {
    e1 = $(this);
    curID = $(this).parent('td').parent('tr').attr('id');       
    e1.autocomplete({
        minLength: 3,
        source: function(request, response) { 
            findSuggestions(request, response, 'artist', artist_cache, curID);
        }
    });
});

As you can see, I get the current selector and put it into e1. There are multiple rows with the given 'artist' input and I want to be able to know the ID for each row in the findSuggestions method but when the method is called, each row is given the same ID (which is the referencing the last of the rows.

Any idea why this might be occurring? Am I approaching this problem incorrectly?

Thanks!

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

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

发布评论

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

评论(2

人心善变 2024-09-17 10:33:15

您需要在闭包内定义变量以赋予其局部作用域,否则您将创建全局变量

$("input[id$=artist]").each(function() {
    var e1 = $(this);
    var curID = $(this).parent('td').parent('tr').attr('id');       
    e1.autocomplete({
        minLength: 3,
        source: function(request, response) { 
            findSuggestions(request, response, 'artist', artist_cache, curID);
        }
    });
});

You need to define your variable inside the closure to give it local scope otherwise you are creating a global variable

$("input[id$=artist]").each(function() {
    var e1 = $(this);
    var curID = $(this).parent('td').parent('tr').attr('id');       
    e1.autocomplete({
        minLength: 3,
        source: function(request, response) { 
            findSuggestions(request, response, 'artist', artist_cache, curID);
        }
    });
});
淡紫姑娘! 2024-09-17 10:33:15

您忘记将 var 放在 curID 变量之前。

这会在窗口对象上创建一个全局变量curID,因此每个源回调函数都引用同一个对象,从而引用同一个值。

更改为

var curID = $(this).parent('td').parent('tr').attr('id');       

永远不要忘记将 var 放在变量声明之前,因为它可能是诸如此类的痛苦错误的根源。

You forgot to put var before the curID variable.

That creates a global variable curID on the window object, so every source callback function is referring to the same object, and thus value.

Change to

var curID = $(this).parent('td').parent('tr').attr('id');       

Never forget to put var before a variable declaration, as it can be the source of painful bugs such as this one.

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