26
<form name="myForm" id="myForm" action="test.php" method="POST">
  <p>
  <input name="test" value="test" />
  </p>
  <p>
    <input type="submit" name="submit" value="Submit" />
  </p>
</form>

    <script>

    var auto_refresh = setInterval(
    function()
    {
    submitform();
    }, 10000);

    function submitform()
    {
      alert('test');
      document.myForm.submit();
    }
    </script>

一旦登陆页面,我尝试每10 秒自动提交一次表单时遇到问题。表单名称是 myForm action="test.php"。我收到'test'消息,但页面未提交表单。

除了在页面加载时自动加载功能之外的任何解决方案?

修复:从提交按钮中删除 ( name="submit") 并且运行顺利。

4

5 回答 5

27

您需要指定一个框架,一个目标,否则您的脚本将在第一次提交时消失!

更改document.myFormdocument.forms["myForm"]

<form name="myForm" id="myForm" target="_myFrame" action="test.php" method="POST">
    <p>
        <input name="test" value="test" />
    </p>
    <p>
        <input type="submit" value="Submit" />
    </p>
</form>

<script type="text/javascript">
    window.onload=function(){
        var auto = setTimeout(function(){ autoRefresh(); }, 100);

        function submitform(){
          alert('test');
          document.forms["myForm"].submit();
        }

        function autoRefresh(){
           clearTimeout(auto);
           auto = setTimeout(function(){ submitform(); autoRefresh(); }, 10000);
        }
    }
</script>
于 2012-09-11T19:05:14.143 回答
11

尝试使用document.getElementById("myForm")而不是 document.myForm。

<script>
var auto_refresh = setInterval(function() { submitform(); }, 10000);

function submitform()
{
  alert('test');
  document.getElementById("myForm").submit();
}
</script>
于 2012-09-11T19:11:21.027 回答
4

延迟自动提交的简单解决方案:

<body onload="setTimeout(function() { document.frm1.submit() }, 5000)">
   <form action="https://www.google.com" name="frm1">
      <input type="hidden" name="q" value="Hello world" />
   </form>
</body>
于 2015-10-08T10:51:48.593 回答
4

这个解决方案对我有用:

<body onload="setTimeout(function() { document.myform.submit() }, 5000)">
   <form action=TripRecorder name="myform">
      <textarea id="result1"  name="res1" value="str1" cols="20" rows="1" ></textarea> <br> <br/>
      <textarea id="result2" name="res2" value="str2" cols="20" rows="1" ></textarea>
   </form>
</body>
于 2016-05-18T15:36:32.463 回答
-6

试试这个,

HtmlElement head = _windowManager.ActiveBrowser.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = _windowManager.ActiveBrowser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "window.onload = function() { document.forms[0].submit(); }";
head.AppendChild(scriptEl);
strAdditionalHeader = "";
_windowManager.ActiveBrowser.Document.InvokeScript("webBrowserControl");
于 2015-04-16T08:20:18.557 回答