使用Javascript动态创建HTML表单

使用Javascript动态创建HTML表单,javascript,html,Javascript,Html,如何使用javascript创建表单 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

如何使用javascript创建表单

 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();
我不知道如何继续,我一直在用谷歌搜索我的脸,但没有什么明确的东西可以告诉我如何用它动态创建表单

 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();

先走一步

使用,或者如果你想更灵活地使用,我认为发布一个完整的解决方案太多了,但是请查看jQuery。
 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();

我给你一个提示,jQuery可能对你非常有用:)

只要使用标准的DOM API(
document.createElement()
,)来创建表单的所有元素。

我的建议是获取库并使用它的函数来创建表单。(如果下划线对您来说太大,模板函数也可以独立运行)。

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

 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();
也许:`

<html>
  <body>
    <h1>My Form</h1>
    <div id="formcontent">
    </div>
  </body>
</html>
 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();

试试这个

 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();
myWin=open(“,”displayWindow“,”宽度=800,高度=500,状态=yes,工具栏=yes,菜单栏=yes”);
myWin.document.open();
myWin.document.write(“V E N U”);
myWin.document.write(“.hdr{……}”);
myWin.document.write(“”);
myWin.document.write(“”);
myWin.document.write(“你想要什么”);
.
.
myWin.document.write(“”);
myWin.document.write(“”);
myWin.document.close();

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

您可以尝试以下方法:

 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();
HTML部分:

 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();
<html>
 <head></head>
 <body>

 <body>
</html>

javascript:

 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();
<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>

//创建表单
var f=document.createElement(“表单”);
f、 setAttribute('method','post');
f、 setAttribute('action','submit.php');
//创建输入元素
var i=document.createElement(“输入”);
i、 type=“text”;
i、 name=“用户名”;
i、 id=“用户名称1”;
//创建一个复选框
var c=document.createElement(“输入”);
c、 type=“checkbox”;
c、 id=“checkbox1”;
c、 name=“check1”;
//创建一个按钮
var s=document.createElement(“输入”);
s、 type=“提交”;
s、 value=“提交”;
//将所有元素添加到表单中
f、 儿童(一);
f、 儿童(c);
f、 儿童;
//将表单添加到主体中
$(“正文”)。附加(f)//使用jQuery或
document.getElementsByTagName('body')[0].appendChild(f)//纯javascript

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

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

基本上,与使用JavaScript创建其他元素一样,使用
createElement
或jQuery。你会用jQuery吗?是的,我会用jQuery,不过我不太熟悉。我想你需要先看看关于javascript的youtube教程。。。查看NetNinja或google chrome开发者,你有很多信息可以回答你的问题:var f=document.createElement(“表单”);如果在脚本执行时没有加载DOM,您可能会得到一个错误-在我的例子中,我得到了“无法读取未定义的属性'appendchild'”。所以最好将javascript代码封装在WindowOnLoad块中。例如,window.onload=function(){//javascript code here}@raj-如果您有问题,那么。不要把你的问题编辑成别人的答案!这充满了错误…变量名不匹配,您将CreateElement和GetElementById大写
 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();