-6ren">
gpt4 book ai didi

Javascript - 提交后一分钟无法提交的表单

转载 作者:行者123 更新时间:2023-11-28 00:21:10 25 4
gpt4 key购买 nike

我必须创建一个表单,该表单在提交后一分钟内无法提交。

<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>

但是怎么办呢?我可以使用某种计时器吗?

最佳答案

  1. 您不应使用点击事件来提交。
  2. 由于页面是通过提交卸载的,因此您需要使用 cookie 来节省时间并隐藏提交按钮:

例如

test page

<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;
}

关于Javascript - 提交后一分钟无法提交的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com