空闲时自动保存表单
Autosave form on idleness
我有下面的代码,它应该在用户空闲时自动提交(保存)一个名为"project"的表单。这是我在教程网站上找到的代码(忘记名字),我试过了,它只刷新了页面?
<!-- Set auto save timeout in milliseconds
<script type="text/javascript">
attachEvent(window,'load',function(){
var idleSeconds = 5;
var idleTimer;
function resetTimer(){
clearTimeout(idleTimer);
idleTimer = setTimeout(whenUserIdle,idleSeconds*1000);
}
attachEvent(document.body,'mousemove',resetTimer);
attachEvent(document.body,'keydown',resetTimer);
attachEvent(document.body,'click',resetTimer);
resetTimer(); // Start the timer when the page loads
});
function whenUserIdle(){
document.project.submit();
window.location = location.href;
}
function attachEvent(obj,evt,fnc,useCapture){
if (obj.addEventListener){
obj.addEventListener(evt,fnc,!!useCapture);
return true;
} else if (obj.attachEvent){
return obj.attachEvent("on"+evt,fnc);
}
}
</script> -->
表单代码:
<form name="project" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="invoice-form" method="post" class="invoice-form" role="form" novalidate>
这是刷新页面window.location = location.href;
的代码。尝试删除它。
您还需要确保表单的属性name
正在替换document.project.submit();
中的"项目"。
例如
<form name="test_form"></form>
document.test_form.submit();
编辑:
好的,函数应该只是
function whenUserIdle() {
document.project.submit();
}
与其进行实际的表单提交,不如考虑进行AJAX请求:
function whenUserIdle(){
var formData = {}; // assemble the form data here
$.post( "/form-submission-route", formData, function( responseData) {
// do something with result, if you like
// perhaps clear the form or throw up a notification
});
}
相关文章:
- 如何停止javascript的执行并允许用户保存表单
- javascript html使用webstorage保存表单
- 在浏览器会话中保存表单数据
- 空闲时自动保存表单
- 是否可以在没有用户提交表单的情况下保存表单数据
- 如何在单击“另存为草稿”按钮时使用 JavaScript 保存表单内容
- PHP 页面未保存表单和解析输出
- 使用 jquery 记住保存表单时打开的选项卡
- 使用ajax调用自动保存表单数据
- 如何在浏览器中保存表单输入
- 保存表单输入数据以在其他页面上使用
- Don't保存表单
- 如何在模态窗口中保存表单值
- 仅在提交时使用 javascript 保存表单状态
- 我如何保存表单数据与jquery和php sql
- 单击按钮保存表单中的数据
- 如何使用 HTML5 本地存储保存表单中的数据
- 如果用户没有保存表单,如何测试用户是否对表单进行了任何更改
- 如何保存表单中的信息?
- 通过ajax保存表单数据,不需要服务器代码