JavaScript-使用本地时间提交表单

JavaScript - Submit Form with localtime

本文关键字:时间 提交 表单 JavaScript-      更新时间:2023-09-26

我正在尝试将提交表单和日期/本地时间结合起来

从每次"15:30:00"提交

这是我的代码js:

<script type="text/javascript">
function initClock() 
{
  var now = new Date();
  var hr  = now.getHours();
  var min = now.getMinutes();
  var sec = now.getSeconds();
  if (min < 10) min = "0" + min; 
  if (sec < 10) sec = "0" + sec;
  var time = document.getElementById('clockDisplay');
      time.innerHTML = hr + ":" + min + ":" + sec;
  setTimeout('initClock()', 500);
}
function timesubmit()
{
    if (time.innerHTML == "15:30:00") // time submit 15:30:00
        {
          document.getElementById("myForm").submit();
        } 
    else 
        {
          //etc
        }
}
</script>

我的HTML代码是这样的:

<body onload="initClock()">
<div id="clockDisplay"></div>
<br>
<form id="myForm" action="http://example.com" method="post">
<input type="submit" onclick="timesubmit()" value="send"  />
</form>
</body>

你知道我该怎么做吗?如果时间为"15:30:00",则自动提交表单

感谢所有能帮助我的人:D

检查此项Fiddle

HTML

<div id="clockDisplay"></div>
<br>
<form id="myForm" action="http://example.com" method="post">
<input type="button" onclick="timesubmit()" value="send"  />
</form>

javascript

document.addEventListener("DOMContentLoaded",initClock,false);
var timeStamp;
function initClock() 
{
  var now = new Date();
  var hr  = now.getHours();
  var min = now.getMinutes();
  var sec = now.getSeconds();
  if (hr < 10) hr = "0" + hr; 
  if (min < 10) min = "0" + min; 
  if (sec < 10) sec = "0" + sec;
  timeStamp= hr + ":" + min + ":" + sec;
  var time = document.getElementById('clockDisplay');
  time.innerHTML = timeStamp;
  timesubmit();
  setTimeout(initClock, 0);
}
function timesubmit()
{
    if (timeStamp == "15:30:00") // time submit 15:30:00
        {
          document.getElementById("myForm").submit();
        } 
    else 
        {
          //etc
        }
}

试试这个:

http://jsfiddle.net/wbSbF/1/

变化是:

//so timesubmit can return false to stop the submit action, return true to continue.
onclick="return timesubmit()"
//'initClock()' seemed works fine too, but I prefer just initClock
//setTimeout(initClock, 500);
//define time in timesubmit function
var time = document.getElementById('clockDisplay');