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

发布于 2024-09-30 09:21:45 字数 426 浏览 0 评论 0原文

这是一个简单的带有函数的 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>

提前致谢

This is a simple javascript add row with functions, then I don't know how to limit the additional row e.g. for max. 5 rows, any idea and pointer or example(s) I would be appreciated

<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>

Thanks In advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2024-10-07 09:21:45

添加之前计算行数。行只是表元素中的一个数组 - 您可以通过 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>

Count rows before adding. Rows are just an array within the table element - you can get its length via 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>
め可乐爱微笑 2024-10-07 09:21:45
var counter =1;
var limit = 5;
function addInput(yourDivName){
if (counter==5) {
     *the code you want to execute if the limit is reached* }
else {
     var newdiv = document.createElement('div');
     newdiv.innerHTML = "<span id=''>Serial Number " + (counter + 1) + " : <input type='text' name='myInputs[]'/></span>";
     document.getElementById(divName).appendChild(newdiv);
     counter++;
     }
}
var counter =1;
var limit = 5;
function addInput(yourDivName){
if (counter==5) {
     *the code you want to execute if the limit is reached* }
else {
     var newdiv = document.createElement('div');
     newdiv.innerHTML = "<span id=''>Serial Number " + (counter + 1) + " : <input type='text' name='myInputs[]'/></span>";
     document.getElementById(divName).appendChild(newdiv);
     counter++;
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文