Javascript 这个java脚本代码有什么问题
下面的代码没有在我的html中添加字段。如果我删除新的文本部分,它就可以正常工作Javascript 这个java脚本代码有什么问题,javascript,Javascript,下面的代码没有在我的html中添加字段。如果我删除新的文本部分,它就可以正常工作 function add(){ alert("hi"); var addDiv = document.getElementById('addMoreUl'); var ul=document.createElement('ul'); var li=document.createElement('li'); var newText = document.createEle
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);
}
我做错了什么?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”
之后指定的空格,只需将其删除,然后像newText.setAttribute(“type”,text)一样尝试“type”
代码>
您的代码有问题。如果您检查浏览器控制台,您将看到错误
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?
当你说“不起作用”时,我们猜不出你在说什么。你需要更具体一些。预期的行为是什么?它实际上做了什么?它是否在控制台上产生任何错误?只是一个猜测,在调用setAttribute()
method时尝试使用字符串作为第二个参数。它在小提琴中工作正常。我正在尝试在html页面中添加带有javascript的动态ul。但它没有被添加,而是在我删除上述代码中的新文本块时,工作很好,加上它对我不起作用crome@user3333813请再提供一个帮助,关于添加更多我必须添加的字段,我如何动态删除它们。我是如何检索动态添加的idelements@user3333813在什么样的事件上(输入onchange或onblur..等)您想删除动态创建的元素。我有一个jsp页面,其中显示一个包含4个字段的表单。还有一个“添加更多”按钮。当我单击“添加更多”时,另一个表单应该会出现,因为每次单击“添加更多”时,表单的数量应该会增加。。每一个新的创建表单都应该有一个删除按钮,点击这个按钮就会删除它form@user3333813表单是否有动态生成的ID(即包装在容器中的四个字段)以及该容器的唯一ID。