jQuery的自动完成和变化问题 [英] jQuery Autocomplete and on change Problem

查看:132
本文介绍了jQuery的自动完成和变化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ID文本(#text),一个文本框用了自动完成这样的:

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)如果用户点击一个自动完成选项 - >搜索为

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

b)若显示用户键入的东西,并自动完成动作,但他点击了其他位置 - >搜索该字符串他输入

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

c)如果用户键入的东西,有自动完成,并点击了其他位置(change事件) - >搜索该字符串他输入

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

如果我不使用触发器,比)完全不工作,如果我键入A,然后点击自动完成选项,#text包含一中的更改功能。因此,前值变化大火被改变。我的想法是,这将是没有问题的话后不久,其结果再次闪光触发器现在有了正确的价值,但是这不会发生。

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?

推荐答案

我从来没有与您正在使用,但这里是一个工具,你应该在你的JavaScript解决问题的工具箱中的自动完成的工作。

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.

当立即事件触发之前另一个事件,你需要等待第二个事件来处理第一完成之前,使用的setTimeout(...)。有时候它看起来像一个延迟是必需的,但更多的,往往不是你只需要的setTimeout(...,0)

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

通过上面的code,你可以做这样的事情在你的onChange处理:

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

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

这会叫的setTimeout连续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.

这篇关于jQuery的自动完成和变化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆