好吧,我对 JS 很陌生,人们给了我很大的帮助,但我在创建具有多个输入元素的表单时遇到了问题。第二个元素正在出现,但第一个元素正在填充第二个元素的 input.value,而我在第二个元素中没有得到任何值
我以前没有使用过 DOM 函数,所以我可能搞乱了我如何调用它们。非常感谢任何帮助
newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
newWindow.document.title = "Info Window";
// 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 cNumLab = document.createElement('cNumLab');
form.appendChild(document.createTextNode('Craft Number:'));
// add a text input
var input = document.createElement('input');
input.type = 'text';
input.name = 'input';
input.value = 'Enter Craft Number Here';
form.appendChild(input);
//add linebreak
var linebreak = document.createElement('br');
form.appendChild(linebreak);
// add text before the input
var sDescL = document.createElement('sDescL');
form.appendChild(document.createTextNode('Short Desc:'));
// add a text input
var sDesc = document.createElement('input');
input.type = 'text';
input.name = 'sDesc';
input.value = 'Enter Short Description Here:';
form.appendChild(sDesc);
最佳答案
在第二个“输入”中,变量引用第一个字段“input”,而不是后面的“sDesc”
应该是
// add a text input
var sDesc = document.createElement('input');
sDesc.type = 'text';
sDesc.name = 'sDesc';
sDesc.value = 'Enter Short Description Here:';
form.appendChild(sDesc);
关于JavaScript 无法创建多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23374466/