论坛首页 入门技术论坛

javascript动态创建form表单

浏览 2760 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-13   最后修改:2009-04-06

//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 name="&quot;+elementName+&quot;" type="hidden">");
inputForm.appendChild(newElement);
newElement.value = elementValue;
return newElement;
}
//function that creates the form, adds some elements
//and then submits it
function createFormAndSubmit(){
var submitForm = getNewSubmitForm();
createNewFormElement(submitForm, "field1", "somevalue");
createNewFormElement(submitForm, "field2", "somevalue");
submitForm.action= "someURL";
submitForm.submit();
} </noscript>
<input type="button" value="Click to create form and submit" onclick="createFormAndSubmit()">

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics