gpt4 book ai didi

javascript - 表格应该改变行数/列数?

转载 作者:太空宇宙 更新时间:2023-11-03 18:35:16 26 4
gpt4 key购买 nike

请看下面的代码,表格应该根据下面selected id选项中选择的输入更改行数,但只读取select id的第一个值,行数不会根据选择,你能指出我代码中的错误吗?

http://jsfiddle.net/uTY6n/14/

HTML:

  <div id="scrollingDiv">
<select id="read">
<option value="1">1</option>
<option value="2">2</option></select>

<table id="contentTable" border="1">
<!-- Fill table programmatically -->
</table>
</div>

JavaScript:

  function buildTable()
{
var myTable =document.getElementById("contentTable");
var j=document.getElementById("read").value;
var rows = [];
var cells = [];

for( var i = 0; i < j; i++ )
{
rows[i] = myTable.insertRow(i);
if(i%3==2)rows[i].addClass("everyrow");
cells[i] = [];

for( var x = 0; x < 3 ; x++ )
{
cells[i][x] =document.createElement((x==0)?"th":"td");
cells[i][x].innerHTML = (x==0)?"<input>":"<input>";
rows[rows.length - 1].appendChild(cells[i][x]);
}
}

}
buildTable();

CSS:

  #scrollingDiv
{
border: 1px groove black;
height: 350px;
width: 350px;
background: #ffffff;
overflow: auto;
}

#contentTable
{
height: 350px;
width: 350px;
}
.every3rdrow{
border-bottom:3px solid black;
}

最佳答案

See this demo.

首先,您需要将事件监听器附加到 <select> :

document.getElemenyById('read').addEventListener('change', buildTable);

然后,您需要确保在每次重建之前清空表buildTable()被称为:

function buildTable() {
var myTable =document.getElementById("contentTable");
var j=document.getElementById("read").value;
var rows = [];
var cells = [];

while (myTable.hasChildNodes()) {
myTable.removeChild(myTable.lastChild);
}

...
}

希望这对您有所帮助!

关于javascript - 表格应该改变行数/列数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18860579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com