Javascript - 提交后一分钟无法提交的表单
Javascript - a form that can not be submitted for a minute after submit
我要创建一个提交后一分钟不能提交的表单。
<form id="myForm" action="<%=response.encodeURL(request.getContextPath()
+ " /upisKorisnika.html ")%>" method="POST">
<table id='userInput' class="display" border="0" style="font-family: Arial; font-size: 18px;">
<tr>
<td>
<c:out value="E-mail: " />
</td>
<td>
<input type="text" id="email" name="email">
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<button onClick="submitform()">Submit</button>
</td>
</tr>
</table>
据我所知,这个问题将在 javascript 中得到解决:
<script type="text/javascript">
function submitform(){
document.getElementById("myForm").submit;
}
</script>
但是怎么办?我可以使用某种计时器吗?
- 您不应使用点击事件来提交。
- 由于页面被提交卸载,您需要使用cookie来节省时间并隐藏提交按钮:
例如
<style>
#subbut { display:none }
</style>
<script>
window.onload=function() { // when the page loads
var time = getCookie("lastSubmitted"),diff=0;
if (time) { // we had a cookie
time = +time; // convert to number
diff = new Date().getTime()-time;
}
diff = 60000-diff;
if (diff<0) diff=0;
setTimeout(function() { // show in diff milliseconds
document.getElementById("subbut").style.display="block";//show it
},diff);
document.getElementById("myForm").onsubmit=function() {
document.getElementById("subbut").style.display="none";
var now = new Date().getTime();
setCookie("lastSubmitted",now);
return false; // REMOVE THIS WHEN TESTED
}
}
</script>
<form id="myForm" ...
<td colspan="2" style="text-align: center">
<button type="submit" id="subbut">Submit</button>
</td>
如果您需要,这里有一个 cookie 脚本
// from quirksmode
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
我要创建一个提交后一分钟不能提交的表单。
<form id="myForm" action="<%=response.encodeURL(request.getContextPath()
+ " /upisKorisnika.html ")%>" method="POST">
<table id='userInput' class="display" border="0" style="font-family: Arial; font-size: 18px;">
<tr>
<td>
<c:out value="E-mail: " />
</td>
<td>
<input type="text" id="email" name="email">
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<button onClick="submitform()">Submit</button>
</td>
</tr>
</table>
据我所知,这个问题将在 javascript 中得到解决:
<script type="text/javascript">
function submitform(){
document.getElementById("myForm").submit;
}
</script>
但是怎么办?我可以使用某种计时器吗?
- 您不应使用点击事件来提交。
- 由于页面被提交卸载,您需要使用cookie来节省时间并隐藏提交按钮:
例如
<style>
#subbut { display:none }
</style>
<script>
window.onload=function() { // when the page loads
var time = getCookie("lastSubmitted"),diff=0;
if (time) { // we had a cookie
time = +time; // convert to number
diff = new Date().getTime()-time;
}
diff = 60000-diff;
if (diff<0) diff=0;
setTimeout(function() { // show in diff milliseconds
document.getElementById("subbut").style.display="block";//show it
},diff);
document.getElementById("myForm").onsubmit=function() {
document.getElementById("subbut").style.display="none";
var now = new Date().getTime();
setCookie("lastSubmitted",now);
return false; // REMOVE THIS WHEN TESTED
}
}
</script>
<form id="myForm" ...
<td colspan="2" style="text-align: center">
<button type="submit" id="subbut">Submit</button>
</td>
如果您需要,这里有一个 cookie 脚本
// from quirksmode
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}