提交一个按钮,只提交最后一个表格

时间:2015-04-21 19:31:13

标签: javascript forms

存在两种形式,哪一种是最后一种并不重要,总是提交最后一种。

<script>
    function submitall()
       {
         document.getElementById("form2").submit();
         document.getElementById("form1").submit();
       }
  </script>

1 个答案:

答案 0 :(得分:0)

以这种方式试试

HTML

这些属于form1

<input type="text" id="form11" name="i1" />
<input type="text" id="form12" name="i2" />

让我们说这个属于form2

 <input type="text" id="form21" name="i3" />

Javascript应该看起来像这样

function ajaxRequest(){
     var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
     if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
      for (var i=0; i<activexmodes.length; i++){
       try{
        return new ActiveXObject(activexmodes[i])
       }
       catch(e){
        //suppress error
       }
      }
     }
     else if (window.XMLHttpRequest) // if Mozilla, Safari etc
      return new XMLHttpRequest()
     else
      return false
    }
var mypostrequest=new ajaxRequest()
mypostrequest.onreadystatechange=function(){
 if (mypostrequest.readyState==4){
  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
   document.getElementById("result").innerHTML=mypostrequest.responseText // ajax response
  }
  else{
   alert("An error has occured making the request")
  }
 }
}

function submitForm(page,parameters)
           {
  mypostrequest.open("POST", page, true);
  mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  mypostrequest.send(parameters);
           }
var value1=encodeURIComponent(document.getElementById("form11").value);
var value2=encodeURIComponent(document.getElementById("form12").value);
var value3=encodeURIComponent(document.getElementById("form21").value);
var p1="i1="+value1+"&i2="+value2;
var p2="i3="+value3;
var parameters=[p1,p2];               
    var pages = ['basicform1.php','basicform2.php'];
    for(var i=0;i<pages.length;i++)
    {
       submitForm(pages[i],parameters[i]);
    }