使用JavaScript创建表单

使用JavaScript创建表单,javascript,Javascript,好的,对JS来说还是很新的,不完全确定我在做什么 我想使用java脚本创建一个包含输入表单的新窗口 我能够得到一个基本的窗口,并与一个单一的选项下拉式工作,但我正在努力如何让一个表格的工作 var i, l, options = [{ value: 'fName', text: 'First Name:' }, { value:'lName', text:'Last Name:' }, { value: 'age', text: 'Age:' }, {

好的,对JS来说还是很新的,不完全确定我在做什么

我想使用java脚本创建一个包含输入表单的新窗口

我能够得到一个基本的窗口,并与一个单一的选项下拉式工作,但我正在努力如何让一个表格的工作

var i, l, options = [{
   value: 'fName',
   text: 'First Name:'
},
{
   value:'lName',
   text:'Last Name:'
},

{
   value: 'age',
   text: 'Age:'
},
{
   value:'city:',
   text:'City:'
},
{
   value:'state',
   text:'State:'
},

{
   value:'zCode',
   text:'Zip Code:'
}
                    ],

newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");  

newWindow.document.write("<select onchange='window.opener.setValue(this.value);'>");
for(i=0,l=options.length; i<l; i++) {
    newWindow.document.write("<option value='"+options[i].value+"'>");  
    newWindow.document.write(options[i].text);  
    newWindow.document.write("</option>");
}

newWindow.document.write("</select>");
newWindow.document.title = "Info Window";
变量i、l、选项=[{
值:“fName”,
文本:“名字:”
},
{
值:'lName',
文字:'姓氏:'
},
{
值:“年龄”,
文字:“年龄:”
},
{
值:'city:',
正文:“城市:”
},
{
值:'state',
文本:'State:'
},
{
值:'zCode',
文本:'邮政编码:'
}
],
newWindow=window.open(“,null,“高度=200,宽度=400,状态=yes,工具栏=no,菜单栏=no,位置=no”);
newWindow.document.write(“”);

对于(i=0,l=options.length;i您应该使用DOM元素对象,而不是构建原始HTML。可以向新窗口添加如下表单:

// create a form and set properties
var form = document.createElement('form');
form.action = 'http://google.com';
form.method = 'post';

// insert into the body of the new window
newWindow.document.body.appendChild(form);

// add text before the input
var p = document.createElement('p');
form.appendChild(document.createTextNode('Enter text:'));

// add a text input
var input = document.createElement('input');
input.type = 'text';
input.name = 'test';
input.value = 'this is an input';
form.appendChild(input);

啊哈,这让它更清晰了。2个快速问题1.我在输入框前没有得到名称,2.如何添加多个元素我只是尝试创建了添加元素,但似乎没有添加。然后要在输入之前添加一些文本,只需对
document.createElement()执行相同的操作
,但创建

或标签。请参阅更新的答案。向表单添加多个输入应该可以正常工作,如果仍然有问题,代码是什么样子的?好的,我可以打开第一个输入窗口,但我认为在执行第二个输入窗口时,我弄乱了createElement标志中的某些内容