以下代码未在我的html中添加字段。如果我删除newText部分,它可以正常工作。
function add(){
alert("hi");
var addDiv = document.getElementById('addMoreUl');
var ul=document.createElement('ul');
var li=document.createElement('li');
var newText = document.createElement('input');
newText.setAttribute("name", Name);
newText.setAttribute("type ", text);
newText.setAttribute("value ", Value);
newText.setAttribute("id",box);
li.appendChild(newText);
var newSpan = document.createElement('span');
newSpan.innerText ="Reward Amount:";
li.appendChild(newSpan);
ul.appendChild(li);
addDiv.appendChild(ul);
}
我做错了什么?
答案 0 :(得分:0)
<强> JS 强>
newText.setAttribute("name", Name);
\\newText.setAttribute("type ", text); \\space is there in "type "
\\newText.setAttribute("value ", Value); \\ space is there in "value "
newText.setAttribute("type", text);
newText.setAttribute("value", Value);
newText.setAttribute("id",box);
问题是newText.setAttribute("type ", text);
您在"type "
删除它之后给出的空间并尝试"type"
newText.setAttribute("type", text);
答案 1 :(得分:0)
您的代码有问题。如果你检查浏览器控制台,你会看到错误。
newText.setAttribute("name", Name); // where is Name from? a variable?
newText.setAttribute("type ", text); // should be "type" rather than "type ", I guess text should be 'text' here.
newText.setAttribute("value ", Value); // should be "value" rather than "value ".
newText.setAttribute("id",box); // is box a variable?