JavaScript 无法创建多个输入

标签 javascript forms

好吧,我对 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/

相关文章:

javascript - Web应用程序是否可能存在松耦合?

javascript - 不同页面上的 AngularJS 不同的 ng-app

php - 如何在从下拉列表中选择项目时更新表单 "item name"

javascript - 使用 Javascript 提交表单

javascript函数将上传文件字段设置为默认值

javascript - 错误 : Assertion Failed: The URL '/' did not match any routes in your application

Javascript 转换 "jumps"而不是转换

JavaScript 样式 : don't use wrapper objects for primitive types

php - 数据库查询后将变量从一个 PHP 页面发送到另一页面

forms - Joomla 表单验证缺少错误消息