Home > Article > Web Front-end > How to dynamically create and submit a form using JavaScript_javascript skills
The example in this article describes the implementation method of dynamically creating and submitting a form using JavaScript. Share it with everyone for your reference, the details are as follows:
If the page layout is somewhat complicated, you may need to dynamically create a form in the page. The demonstration of creating a form with JavaScript is as follows:
// JavaScript 构建一个 form function MakeForm() { // 创建一个 form var form1 = document.createElement("form"); form1.id = "form1"; form1.name = "form1"; // 添加到 body 中 document.body.appendChild(form1); // 创建一个输入 var input = document.createElement("input"); // 设置相应参数 input.type = "text"; input.name = "value1"; input.value = "1234567"; // 将该输入框插入到 form 中 form1.appendChild(input); // form 的提交方式 form1.method = "POST"; // form 提交路径 form1.action = "/servlet/info"; // 对该 form 执行提交 form1.submit(); // 删除该 form document.body.removeChild(form1); }
After calling MakeForm, immediately create a Form, submit it, and then remove the Form.
I hope this article will be helpful to everyone in JavaScript programming.