使用 Javascript 动态创建 HTML 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3297143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:04:32  来源:igfitidea点击:

Dynamically create a HTML form with Javascript

javascripthtml

提问by Odyss3us

how can I go about creating a form with javascript?

我该如何使用 javascript 创建表单?

I have NO idea on how to proceed, I have been googling my face off but there is nothing definitive that can show me how to dynamically create forms with it.

我不知道如何继续,我一直在谷歌上搜索我的脸,但没有什么明确的可以告诉我如何用它动态创建表单。

Thanx in advance!

提前谢谢!

采纳答案by yan.kun

I think posting a complete solution would be too much, but check out jQuery for this. I give you a hint, jQuery's .append()could be very useful for you :)

我认为发布完整的解决方案太多了,但请查看 jQuery。我给你一个提示,jQuery 的.append()可能对你非常有用:)

回答by user1451533

You could try something like this:

你可以尝试这样的事情:

The HTML part:

HTML部分:

<html>
 <head></head>
 <body>

 <body>
</html>

The javascript:

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>

This way you can create as many elements as you want dynamically.

通过这种方式,您可以动态创建任意数量的元素。

回答by Ganesh Babu

My idea is that you can use the dformjquery plugin from github to create forms directly by giving input as json data.

我的想法是,您可以使用github 中的dformjquery 插件,通过将输入作为 json 数据直接创建表单。

回答by IanNorton

Yes, you can create any amount of html including forms by running javascript,

是的,您可以通过运行 javascript 来创建任意数量的 html,包括表单,

Perhaps:`

也许:`

<html>
  <body>
    <h1>My Form</h1>
    <div id="formcontent">
    </div>
  </body>
</html>

Our javascript might look like:

我们的 javascript 可能看起来像:

var e1 = document.CreateElement("input");
el.type = "text";
el.name = "myformvar";

var cont = document.GetElementById("formcontent")
cont.appendChild(e1);

回答by MuniR

TRY THIS...

尝试这个...

 myWin = open("", "displayWindow", "width=800,height=500,status=yes,toolbar=yes,menubar=yes");

                myWin.document.open();
                myWin.document.write("<html><head><title>V E N U S  </title>");
                myWin.document.write("<style type='text/css'>.hdr{.......}</style>");
                     myWin.document.write("</head><body onload='window.print()'>");
                          myWin.document.write("<div class='hdr'>");

                              myWin.document.write("what ever you want");
                              .
                              .


                         myWin.document.write("</div>");               
                     myWin.document.write("</body></html>");
                myWin.document.close();

that's will create a form with DIV inside it...

这将创建一个包含 DIV 的表单......

回答by Aaron Digulla

Just use the standard DOM API (document.createElement(), see these docs for an example) to create all the elements of the form.

只需使用标准DOM API( document.createElement()看到这些文档为例),以创建表单中的所有元素。

回答by Sean Vieira

My recommendation would be to get the underscore.jslibrary and use its templatefunction to create your forms. (If underscore is too large for you, the template function can stand on its own too).

我的建议是获取underscore.js库并使用其template功能来创建表单。(如果下划线对你来说太大了,模板函数也可以独立存在)。

回答by zaynyatyi

use document.writeor if you want more flexibility use jquery

使用document.write或者如果你想要更大的灵活性使用jquery