javascript 如何提交表单 onkeyup 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4069326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to submit form onkeyup action
提问by Josh R
I am trying to save the form onkeyup action. I am new to jquery.
我正在尝试保存表单 onkeyup 操作。我是 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.
编辑1:保存表单意味着保存到服务器。有没有办法增加 0.2 秒的延迟。
回答by Lorenzo
This code will submit your form on keyup
此代码将在 keyup 上提交您的表单
$('#element').bind('keyup', function() {
$('#form').delay(200).submit();
});
In this code you intercept the form submit and change it with an ajax submit
在此代码中,您拦截表单提交并使用 ajax 提交更改它
$("#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.
要使用延迟功能,您应该使用 jQuery 1.4。传递给延迟的参数以毫秒为单位。
回答by Pascal Qyy
From this jQuery forum thread:
$('#element').bind('keyup', function() { $('#form').submit(); } );
回答by Alfredo Lingoist Jr.
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>

