2011-05-20 39 views
0

當我點擊「提交第二個」時,頁面會轉到first.html。我想它去second.html在表單中提交表格

的index.html

<body onload="secondForm();"> 
<script type="text/javascript"> 
function ajaxRequest(){ 
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] // activeX versions in IE 
if (window.ActiveXObject){ // test support for ActiveXObject in IE 
    for (var i=0; i<activexmodes.length; i++){ 
     try{ 
      return new ActiveXObject(activexmodes[i]) 
     } 
     catch(e){ 
      // suppress 
     } 
    } 
} 
else if (window.XMLHttpRequest) // mozilla,safari,chrome,opera 
    return new XMLHttpRequest() 
else 
    return false 
} 
function secondForm(id) { 
var mygetrequest=new ajaxRequest() 
mygetrequest.onreadystatechange=function() { 
    if (mygetrequest.readyState==4) { 
     if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1) { 
      document.getElementById("secondForm").innerHTML=mygetrequest.responseText 
     } 
    } 
} 
mygetrequest.open("POST", "secondForm.html, true) 
mygetrequest.send(null) 
} 
</script> 

<form method="POST" action="first.html" id="firstForm"> 
<span id="secondForm"></span> 
<input type="submit" value="Submit First"> 
</form> 

secondForm.html

<form method="POST" action="second.html" id="secondForm"> 
<input type="submit" value="Submit Second"> 
</form> 
+0

這些ajax調用中的任何一個被調用?此外,您打算如何執行此代碼?你也有2件事情用id'secondForm',你不能有2件東西在DOM中具有相同的ID,它需要是唯一的 – Neal 2011-05-20 12:17:26

+0

你使用jquery還是可以使用它,如果推薦 – 2011-05-20 12:17:53

+0

它在 – reefine 2011-05-20 12:18:45

回答

1

您在表單中有一個表單,外部表單會在提交時處理。

此外,我會建議使用庫爲您的Ajax請求,我使用jQuery:

$('#secondForm').load('secondForm.html'); 

更改您的HTML所以這會工作:

<form method="POST" action="first.html" id="firstForm"> 
    <input type="submit" value="Submit First"> 
</form> 

<span id="secondForm"></span> 

secondForm.html

<form method="POST" action="second.html" id="secondFormID"> <-- use a different ID 
    <input type="submit" value="Submit Second"> 
</form> 
5

不能嵌套形式。您必須更改第一個表單的動作url。