如何通过 Javascript 动态创建表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6964927/
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
How to Create a Form Dynamically Via Javascript
提问by Nick
My sites Aweber registration forms are getting a lot of spam. I was told to create the forms dynamically via javascript after page has rendered or via clicking button. How would I create and render a form via javascript?
我的网站 Aweber 注册表中收到大量垃圾邮件。我被告知在页面呈现后或通过单击按钮通过 javascript 动态创建表单。我将如何通过 javascript 创建和呈现表单?
回答by Pheonix
some thing as follows ::
一些事情如下::
Add this After the body tag
在 body 标签之后添加这个
This is a rough sketch, you will need to modify it according to your needs.
这是一个粗略的草图,您需要根据需要对其进行修改。
<script>
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");
var i = document.createElement("input"); //input element, text
i.setAttribute('type',"text");
i.setAttribute('name',"username");
var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"submit");
s.setAttribute('value',"Submit");
f.appendChild(i);
f.appendChild(s);
//and some more input elements here
//and dont forget to add a submit button
document.getElementsByTagName('body')[0].appendChild(f);
</script>