How to submit form onkeyup action
I am t开发者_高级运维rying 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.
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.
From this jQuery forum thread:
$('#element').bind('keyup', function() { $('#form').submit(); } );
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>
精彩评论