在表格中提交表格

时间:2011-05-20 12:14:51

标签: javascript forms

当我点击“提交第二个”时,页面会转到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>

2 个答案:

答案 0 :(得分:5)

您无法嵌套表单。您必须更改第一个表单的操作URL。

答案 1 :(得分: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>