我有这个用于动态 HTML 的代码,但默认情况下我无法显示它,当我选择选项 jsfiddle 时正在显示表格:http: //jsfiddle.net/4DYCm/
HTML:
<tr>
<th>Number of Models:<title="Number of Models"></th>
<td><select id="numbermodels" name="numbermodels" autocomplete="off" onchange="buildTable(this.value);" >
<option value="1" >1</option >
<option value="2" >2</option >
<option selected="selected" value="3" >3</option >
<option value="4" >4</option >
<option value="5" >5</option >
<option value="6">6</option >
</select></td>
</tr>
<table id="contentTable" border="1" name="contentTable">
<!-- Fill table programmatically -->
</table>
JavaScript:
function buildTable(val)
{
var myTable =document.getElementById("contentTable");
var j=val;
var rows = [];
var cells = [];
while (myTable.hasChildNodes()) {
myTable.removeChild(myTable.lastChild);
}
for( var i = 0; i < 1; i++ )
{
rows[i] = myTable.insertRow(i);
if(i%3==2)rows[i];
cells[i] = [];
for( var x = 0; x < j ; x++ )
{
cells[i][x] =document.createElement((x==0)?"th":"td");
cells[i][x].innerHTML = (x==0)?"<input id=t name=t[] size=3>":"<input id=t1 name=t1[] size=3>";
rows[rows.length - 1].appendChild(cells[i][x]);
}
}
}
buildTable();
我尝试给 onload 代替 onchange,但是当我给它时,表格没有生成,有人可以告诉我如何默认显示这个表格吗?