jQuery 自动完成和更改问题

发布于 2024-10-17 15:30:01 字数 676 浏览 1 评论 0原文

我有一个带有 id 文本(“#text”)的文本框,并具有这样的自动完成功能:

$("#text").autocomplete(data);

这工作正常。 现在我想要三件事:

a)如果用户单击自动完成选项 - >搜索

b) 如果用户键入某些内容并显示自动完成操作,但他单击其他位置 ->搜索他输入的字符串

c) 如果用户输入必须自动完成的内容并单击其他位置(更改事件)->搜索他输入的字符串

对我来说听起来很容易,但我无法让它工作。目前我有类似的情况:

$("#text").result(function(e) {
    $("#text").trigger("change");
});
$("#text").change(function(e){
    $(".x").load(...);  
});

如果我不使用该触发器,则 a) 根本不起作用,如果我输入“a”并单击自动完成选项,“#text”在更改功能中包含“a” 。因此,更改会在值更改之前触发。我的想法是,这不会有问题,因为不久之后结果会以正确的值再次触发触发器,但这种情况不会发生。

像这样,它有时有效,但并非总是有效。我尝试了很多很多的东西,有些效果更好,有些效果更差,但没有什么总是正确的。我该如何以正确的方式做到这一点?

I have a Textbox with the id text ("#text") with has an autocomplete like that:

$("#text").autocomplete(data);

this works fine.
Now i want three things:

a) If the user clicks on a autocomplete option -> search for that

b) If the user types something and an autocomplete action is shown but he clicks somewhere else -> search for that string he typed

c) If the user types something that has to autocomplete and clicks somewhere else (change event) -> search for that string he typed

Sounds easy to me but i cant get it to work. At the Moment i have something like that:

$("#text").result(function(e) {
    $("#text").trigger("change");
});
$("#text").change(function(e){
    $(".x").load(...);  
});

If i dont use that trigger, than a) does not work at all, if i type "a" and click on an autocomplete option, "#text" contains "a" in the change function. So the change fires before the value is changed. My thought is that this would be no problem as then shortly after that the result fires the trigger again now with the right value, but that does not happen.

Like this it sometimes works but not all the time. I tried lots and lots of stuff and some worked better and some worse but nothing was correct all the time. How do i do this the right way?

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

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

发布评论

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

评论(1

狼性发作 2024-10-24 15:30:01

我从未使用过您正在使用的自动完成功能,但这里有一个您应该包含在 JavaScript 问题解决工具箱中的工具。

当一个事件在另一个事件之前立即触发并且您需要等待第二个事件完成才能处理第一个事件时,请使用 setTimeout(...)。有时看起来需要延迟,但通常情况下,您只需要对 setTimeout(..., 0) 进行一些嵌套调用

function onIdle(fn, loops) {
  loops = loops == null ? 1 : loops;
  if (loops > 0) {
    setTimeout(function() { onIdle(fn, loops - 1); }, 0);
  } else {
    fn();
  }
}

使用上面的代码,您可以在 onChange 处理程序中执行类似的操作:

onIdle(function() { ... }, 5);

这将连续调用 setTimeout 5 次。第五次(此后其他事件应该已完成),您可以处理更改并使用新值。

I've never worked with the autocomplete that you're using, but here's a tool you should include in your JavaScript problem-solving toolbox.

When an event fires immediately before another event and you need to wait for the second event to complete before processing the first, use setTimeout(...). There are times where it looks like a delay is required, but more often than not, you just need a few nested calls to setTimeout(..., 0)

function onIdle(fn, loops) {
  loops = loops == null ? 1 : loops;
  if (loops > 0) {
    setTimeout(function() { onIdle(fn, loops - 1); }, 0);
  } else {
    fn();
  }
}

With the above code, you could do something like this in your onChange handler:

onIdle(function() { ... }, 5);

That would call setTimeout consecutively, 5 times. On the fifth time (after which the other event should have completed), you can process the change and use the new value.

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