Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit form onkeyup action

I am trying to save the form onkeyup action. I am new to jQuery.

Is this possible?

I appreciate any help.

edit 1: Save the form means save to server. Is there a way to add 0.2 seconds delay.

like image 305
Josh R Avatar asked Nov 01 '10 13:11

Josh R


3 Answers

This code will submit your form on keyup

$('#element').bind('keyup', function() { 
    $('#form').delay(200).submit();
});

In this code you intercept the form submit and change it with an ajax submit

$("#form").submit(function (event) {
    event.preventDefault();
    $.ajax({
        type: "post",
        dataType: "html",
        url: '/url/toSubmit/to',
        data: $("#form").serialize(),,
        success: function (response) {
            //write here any code needed for handling success         }
    });
});

To use the delay function you should use jQuery 1.4. The parameter passed to delay is in milliseconds.

like image 83
Lorenzo Avatar answered Sep 20 '22 13:09

Lorenzo


From this jQuery forum thread:

$('#element').bind('keyup', function() { $('#form').submit(); } );
like image 42
Pascal Qyy Avatar answered Sep 21 '22 13:09

Pascal Qyy


This is my solution:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt-br" lang="pt-br">
<head><title>Submit after typing finished</title>
<script language="javascript" type="text/javascript">
function DelayedSubmission() {
    var date = new Date();
    initial_time = date.getTime();
    if (typeof setInverval_Variable == 'undefined') {
            setInverval_Variable = setInterval(DelayedSubmission_Check, 50);
    } 
}
function DelayedSubmission_Check() {
    var date = new Date();
    check_time = date.getTime();
    var limit_ms=check_time-initial_time;
    if (limit_ms > 800) { //Change value in milliseconds
        alert("insert your function"); //Insert your function
        clearInterval(setInverval_Variable);
        delete setInverval_Variable;
    }
}

</script>
</head>
<body>

<input type="search" onkeyup="DelayedSubmission()" id="field_id" style="WIDTH: 100px; HEIGHT: 25px;" />

</body>
</html>
like image 21
Alfredo Lingoist Jr. Avatar answered Sep 19 '22 13:09

Alfredo Lingoist Jr.