gpt4 book ai didi

javascript - 限制添加行功能不起作用,请帮助

转载 作者:行者123 更新时间:2023-11-28 02:49:34 25 4
gpt4 key购买 nike

这是一个简单的带有函数的javascript添加行,那么我不知道如何限制附加行,例如对于最大。 5 行,任何想法和指针或示例我将不胜感激

<script type="text/javascript">
function addRow() {
var newRow = document.all("tblGrid").insertRow();
oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t1'><input type='button' value='Delete' onclick='removeRow(this);'/>";
//if(oCell>=5)return;
}
</script>

提前致谢

最佳答案

添加之前计算行数。行只是表元素中的一个数组 - 您可以通过 length

获取其长度
<script type="text/javascript">
function addRow() {
if (document.all("tblGrid").rows.length == 5) {
return; // already max 5 rows
}
var newRow = document.all("tblGrid").insertRow();
oCell = newRow.insertCell();
oCell.innerHTML = "<input type='text' name='t1'><input type='button' value='Delete' onclick='removeRow(this);'/>";
}
</script>

关于javascript - 限制添加行功能不起作用,请帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4074998/

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