Javascript动态创建FORM提交
<script language="javascript" type="text/javascript">
<!--
//function that creates the form, adds some elements
//and then submits it
function select(id)
{
var t = document.getElementById("t" + id);
var d = document.getElementById("d" + id);
var submitForm = getNewSubmitForm();
createNewFormElement(submitForm, "name", t.innerHTML);
createNewFormElement(submitForm, "remark", d.innerHTML);
submitForm.action= "quote.aspx";
submitForm.submit();
}
//helper function to create the form
function getNewSubmitForm()
{
var submitForm = document.createElement("FORM");
document.body.appendChild(submitForm);
submitForm.method = "POST";
return submitForm;
}
//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue)
{
var newElement = document.createElement("input");
newElement.setAttribute("name",elementName);
newElement.setAttribute("type","hidden");
newElement.setAttribute("value",elementValue);
inputForm.appendChild(newElement);
return newElement;
}
//-->
</script>
<!--
//function that creates the form, adds some elements
//and then submits it
function select(id)
{
var t = document.getElementById("t" + id);
var d = document.getElementById("d" + id);
var submitForm = getNewSubmitForm();
createNewFormElement(submitForm, "name", t.innerHTML);
createNewFormElement(submitForm, "remark", d.innerHTML);
submitForm.action= "quote.aspx";
submitForm.submit();
}
//helper function to create the form
function getNewSubmitForm()
{
var submitForm = document.createElement("FORM");
document.body.appendChild(submitForm);
submitForm.method = "POST";
return submitForm;
}
//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue)
{
var newElement = document.createElement("input");
newElement.setAttribute("name",elementName);
newElement.setAttribute("type","hidden");
newElement.setAttribute("value",elementValue);
inputForm.appendChild(newElement);
return newElement;
}
//-->
</script>
注意document.createElement("FORM")来创建节点,在IE和Firefox下都可以
IE有3种方式都可以创建一个元素:
1 document.createElement("<input type=text>")
2 document.createElement("<input>")
3 document.createElement("input")
Firefox只支持一种方式:
document.createElement("input");document.setAttribute(name,value);
--------------
注: 在一个节点下增加子节点 IE也比Firefox下的方式要多.
IE:
1 node.insertBefore(Element)
2 node.insertAfter(Element)
3 node.appendChild(Element)
而Firefox仅支持 node.appendChild.