简体   繁体   English

如何提交表单 onkeyup 操作

[英]How to submit form onkeyup action

I am trying to save the form onkeyup action.我正在尝试保存表单onkeyup操作。 I am new to jQuery.我是 jQuery 的新手。

Is this possible?这可能吗?

I appreciate any help.我很感激任何帮助。

edit 1: Save the form means save to server.编辑1:保存表单意味着保存到服务器。 Is there a way to add 0.2 seconds delay.有没有办法增加 0.2 秒的延迟。

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.要使用延迟功能,您应该使用 jQuery 1.4。 The parameter passed to delay is in milliseconds.传递给延迟的参数以毫秒为单位。

这个 jQuery 论坛线程

$('#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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM