0

我正在使用以下 JavaScript 根据下拉菜单中的数字添加多个输入。它在 Firefox 和 chrome 中运行良好,但在 IE7 中,输入全部浮动在新行上。

function add(number) 
{
var foo = document.getElementById("listInputs2");
var count = 1;
foo.innerHTML = "";

while ( count <= (number) )
{
//Create an input type dynamically.
var element = document.createElement("input");

//Assign different attributes to the element.
element.setAttribute("name", "value"+count);
element.setAttribute("id", "value"+count);
element.setAttribute("size", "60");
element.setAttribute("type", "text");
element.setAttribute("value", "");

//Append the element in page (in span).
foo.innerHTML+=('<li class="inputCount"><label for="value'+count+'">#'+count+'</label>');
foo.appendChild(element);
foo.innerHTML+=("</li>");
count += 1;
}

}
4

1 回答 1

0

我在 Firefox 中遇到了同样的错误:您将输入添加为 listInput2 的子项,而不是 li。

这是一段有效的代码:

function add(number) 
{
var foo = document.getElementById("listInputs2");
var count = 1;
foo.innerHTML = "";

while ( count <= (number) )
{
//Create an input type dynamically.
var input = "<input name='value"+count+"' id='value"+count+"' size='60' type='text' value=''>";

//Append the element in page (in span).
foo.innerHTML+='<li class="inputCount"><label for="value'+count+'">#'+count+'</label>'+input+'</li>';
count += 1;
}

}
于 2010-01-03T08:22:11.417 回答