我有以下代码应该自动提交(保存)名称为" 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>
答案 0 :(得分:1)
这是刷新页面的代码window.location = location.href;
尝试删除它。
您还需要确保您的表单属性name
正在替换&#34;项目&#34;在document.project.submit();
。
例如
<form name="test_form"></form>
document.test_form.submit();
编辑:
好吧,该功能应该只是
function whenUserIdle() {
document.project.submit();
}
答案 1 :(得分:0)
不要进行实际的表单提交,而是考虑执行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
});
}