使用jquery的最后一个密钥之后的密钥

时间:2022-04-03 00:03:11

I'm coding a simple script to extract database informations on input keyup event.

我正在编写一个简单的脚本来提取输入keyup事件的数据库信息。

The problem i have is that the keyup event is always repeated everytime the user press a key. How can i make it working only after the last key pressed?

我遇到的问题是每次用户按键时都会重复键盘事件。如何在按下最后一个按键后才能使其工作?

I think i should use a function like setTimeOut() after every keyup, But i don't know how... can you make me a simple example please?

我想我应该在每个键盘后使用像setTimeOut()这样的函数,但我不知道怎么样...你能给我一个简单的例子吗?

Sorry for my bad english :)

对不起,我的英语不好 :)

Here is what i was trying to do:

这是我想要做的:

$("input").keyup(function()
{
    var timer=setTimeout(function() {
    }, 1000);
    if(!timer)
    {
        //do .post ajax request
    }
});

3 个解决方案

#1


39  

var timer;

$("input").on('keyup', function() {
    clearTimeout(timer);  //clear any running timeout on key up
    timer = setTimeout(function() { //then give it a second to see if the user is finished
        //do .post ajax request //then do the ajax call
    }, 1000);
});

#2


7  

You're basically there. You just need to clear the timer if they enter another letter; otherwise, your callback will execute after the 1000 ms regardless of whether they keep typing.

你基本上就在那里。如果他们输入另一个字母,您只需要清除计时器;否则,你的回调将在1000毫秒后执行,无论他们是否继续输入。

Something like this should work:

像这样的东西应该工作:

$("input").keyup(function (event)
{
    var self = this;
    if (self.timer)
        clearTimeout(self.timer);

    self.timer = setTimeout(function ()
    {
        self.timer = null;
        alert(self.value);
    }, 1000);

});

Where I just have the alert, you'd put your ajax code to write back to the server.

我只是有警报,你会把你的ajax代码写回服务器。

#3


0  

I think if you do this with onchange event you don't need to put time interval checks. whenever you complete your input and change the focus. it triggers onchange event for you

我想如果你使用onchange事件执行此操作,则不需要进行时间间隔检查。每当您完成输入并更改焦点时。它为您触发onchange事件

$("input").onchange(function()
{
   //extract database info
}

#1


39  

var timer;

$("input").on('keyup', function() {
    clearTimeout(timer);  //clear any running timeout on key up
    timer = setTimeout(function() { //then give it a second to see if the user is finished
        //do .post ajax request //then do the ajax call
    }, 1000);
});

#2


7  

You're basically there. You just need to clear the timer if they enter another letter; otherwise, your callback will execute after the 1000 ms regardless of whether they keep typing.

你基本上就在那里。如果他们输入另一个字母,您只需要清除计时器;否则,你的回调将在1000毫秒后执行,无论他们是否继续输入。

Something like this should work:

像这样的东西应该工作:

$("input").keyup(function (event)
{
    var self = this;
    if (self.timer)
        clearTimeout(self.timer);

    self.timer = setTimeout(function ()
    {
        self.timer = null;
        alert(self.value);
    }, 1000);

});

Where I just have the alert, you'd put your ajax code to write back to the server.

我只是有警报,你会把你的ajax代码写回服务器。

#3


0  

I think if you do this with onchange event you don't need to put time interval checks. whenever you complete your input and change the focus. it triggers onchange event for you

我想如果你使用onchange事件执行此操作,则不需要进行时间间隔检查。每当您完成输入并更改焦点时。它为您触发onchange事件

$("input").onchange(function()
{
   //extract database info
}