2017-02-15 56 views
-1

我有一个脚本,它获取了一个表单,其中填充了字段,我得到了一个代码,它会自动每隔x秒提交一次表单。JavaScript窗体打开“无限”选项卡

问题是我添加了这个属性(target =“_ blank”)到窗体上,但窗体一直执行代码并无限制地创建一个新窗体。

我想让我的脚本创建一个新的选项卡来处理表单,第二次执行我的脚本,以使用相同的选项卡刷新处理页面。

我可以在JavaScript中做到这一点吗?

<form target="_blank" name="myForm" id="myForm" action="process.asp" method="post"> 
     field 1:<input type="text" name="field1" id="field1" /><br> 
     field 2:<input type="text" name="field2" id="field2" /><br> 
    </form> 

    <script type="text/javascript"> // code which executes the submit of form operation 
      window.onload=function(){ 
       var auto = setTimeout(function(){ autoRefresh(); }, 100); 

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

       function autoRefresh(){ 
        clearTimeout(auto); 
        auto = setTimeout(function(){ submitform(); autoRefresh(); }, 10000); 
       } 
      } 
    </script>` 

回答

0

请试用此代码(使用Ajax)。它会帮助你使用ajax,它不会打开任何新标签页,并帮助您使用最近更新的日期和时间提交表单。

它在我的基地工作。

请使用您的密码进行操作。

<html> 
    <head> 

    </head> 
    <body> 
     <form target="_self" name="myForm" id="myForm" action="process.asp" method="post"> 
      field 1:<input type="text" name="field1" id="F1" /> 
      <br> 
      field 2:<input type="text" name="field2" id="F2" /> 
      <br> 
      <p id="LastDt"></p> 
     </form> 

     <script type="text/javascript"> // code which executes the submit of form operation 
      window.onload = function() { 
       var auto = setTimeout(function() { 
        autoRefresh(); 
       }, 100); 

       function submitform() { 

        if (window.XMLHttpRequest) 
        { 
         // code for IE7+, Firefox, Chrome, Opera, Safari 
         xmlhttp = new XMLHttpRequest(); 
        } else { 
         // code for IE6, IE5 
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        xmlhttp.onreadystatechange = function() { 
         if (this.readyState == 4 && this.status == 200) { 
          document.getElementById("LastDt").innerHTML = "Last Update : " + new Date(); 
         } 
        }; 

        // It send data on process.asp (silently). 
        xmlhttp.open("POST", "process.asp?field1=" + document.getElementById("F1").value + "&field2=" + document.getElementById("F2").value , true); 
        xmlhttp.send(); 
       } 


       function autoRefresh() { 
        clearTimeout(auto); 
        auto = setTimeout(function() { 
         submitform(); 
        }, 10000); 
       } 
      } 

     </script> 
    </body> 
</html> 
0

你可以在process.asp添加相同的形式和第一后提交打开新标签...使用postMessage API将数据传递到其他标签,并把它从那里提交。

或者你也可以将数据存储在localStorage的,并使用storage eventsprocess.asp监听更新和发布数据

相关问题