2017-02-15 53 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監聽更新和發佈數據

相關問題