在关键最多VS在改变使用AJAX查询:哪个更好? [英] On Key Up vs On Change with AJAX queries: which is better?

查看:100
本文介绍了在关键最多VS在改变使用AJAX查询:哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从文本框发送POST数据到PHP页面进行处理。而不是点击保存按钮的用户,AJAX被用在文本框的事件。

I'm sending POST data from text boxes to a PHP page for processing. Rather than the user clicking a save button, AJAX is used on text box events.

我使用的onChange或的onkeyup之间徘徊。

I'm torn between using onChange or onKeyUp.

的onkeyup需要在服务器上更多的处理,因为每个键preSS我的剧本有访问数据库,并进行更改。服务器的性能可能是一个问题。

OnKeyUp requires more processing on server, because for every key press my script has to access the database and make the change. Server performance could be an issue.

的OnChange是preferred,因为它仅发送变化时用户完成包装盒上。然而,有一个问题,即如果使用不取消选择该文本框,事件'的onChange'不发生,因此,不保存更改

OnChange is preferred, as it only sends the changes when the user has finished on the box. However, there is a problem that if the use does not deselect the text box, the event 'onChange' doesn't happen, so changes aren't saved.

这将是最好的?或者是有没有办法强制执行的onChange不会取消选择框中的用户?

Which would be best? Or is there a way to enforce 'onChange' without the user deselecting the box?

推荐答案

您甚至可以使用的KeyUp preSS Ajax调用,怎么样? 只需添加在keyUp事件的delayKeyUp功能,delayKeyUp功能有setTimeout的方法,这将真正帮助你按的服务器处理的关注,

You can even use the KeyUp press ajax call, how?? Just add the delayKeyUp function on keyUp event, and delayKeyUp function has setTimeout method, this would really help you out as per as server processing concern,

注意:这一次工作正常的一种方法

Note: This works fine for one method at a time.

例。 code:

更新:

$("selector").live('keyup', function(e) { //if jquery v1.7 or more then you can use .on event instead of .live
    delayKeyUp(function(){ ---Your Server Call Stuff -----}, 350); //delay of 350ms.
    return false;
});


var delayKeyUp = (function(){
     var timer = 0;
     return function(callback, ms){
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
     };
})();

这篇关于在关键最多VS在改变使用AJAX查询:哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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