开发者

jQuery form auto submission

开发者 https://www.devze.com 2023-01-28 22:11 出处:网络
I am trying to write an autosubmit form (similar to google instant), and right now I am using this script to do so:

I am trying to write an autosubmit form (similar to google instant), and right now I am using this script to do so:

$(document).ready(function() {
    $('#string').change(function(){
        var url = $(this).val();
        $('#area').load('cgi/test.pl',{string:url});
    });
});

Now the problem with this is that after use finishes typing he will need to click outside the input field. I have tried using onkeyup etc. instead of change but firstly that will result in insane amount of requests, and secondly if you paste text in the field, it won't work. I have also tried to use javascript setTimeout with combination of the script, but couldn't really g开发者_StackOverflowet it working.

Basically what I am after is autosubmission script that 'detects' when user is still typing (so some kind of delay for the submission) and also can detect when text has been pasted to the input field.

Cheers for any suggestions.


I would use the doTimeout plugin to implement "debouncing" (example: http://benalman.com/code/projects/jquery-dotimeout/examples/debouncing/).

This way, you can use keyup. To account for users pasting text, keep the change handler; just invoke the keyup handler when change fires.


Why didn't setTimeout work? Try doing something like this:

$(document).ready(function() {
    var currentTimeout = null;

    $('#string').keyup(handler);
    $('#string').change(handler);

    var handler = function() {    
        if(currentTimeout != null) {
           clearTimeout(currentTimeout);
        }

        currentTimeout = setTimeout(function() {
           var url = $(this).val();
           $('#area').load('cgi/test.pl',{string:url});
        }, 750);
    };
});
0

精彩评论

暂无评论...
验证码 换一张
取 消