如何使用 javascript 创建表单?
我不知道如何继续,我一直在谷歌上搜索我的脸,但没有任何确定的东西可以告诉我如何用它动态创建表单。
提前致谢!
最佳答案
你可以尝试这样的事情:
HTML 部分:
<html>
<head></head>
<body>
<body>
</html>
JavaScript:
<script>
//create a form
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");
//create input element
var i = document.createElement("input");
i.type = "text";
i.name = "user_name";
i.id = "user_name1";
//create a checkbox
var c = document.createElement("input");
c.type = "checkbox";
c.id = "checkbox1";
c.name = "check1";
//create a button
var s = document.createElement("input");
s.type = "submit";
s.value = "Submit";
// add all elements to the form
f.appendChild(i);
f.appendChild(c);
f.appendChild(s);
// add the form inside the body
$("body").append(f); //using jQuery or
document.getElementsByTagName('body')[0].appendChild(f); //pure javascript
</script>
通过这种方式,您可以动态创建任意数量的元素。
关于javascript - 使用 Javascript 动态创建 HTML 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3297143/