【问题标题】:limit add row in function not work, please help限制在功能中添加行不起作用,请帮助
【发布时间】:2010-11-02 04:53:26
【问题描述】:

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

提前致谢

【问题讨论】:

    标签: javascript function limit add


    【解决方案1】:

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

    【讨论】:

    • @user - 欢迎您。随意遵循通常的 stackoverflow 自定义,如果答案有帮助,请单击旁边的复选标记接受它,并单击向上箭头进行投票。谢谢!
    • 我做到了,但是当我单击向上箭头时,会出现消息“投票需要 15 个声誉”,所以我失败了。对不起,我是 stackoverflow 的新手,我也不知道如何提高我的声誉。
    【解决方案2】:
    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++;
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 2012-11-22
      • 2018-03-16
      • 2020-01-07
      相关资源
      最近更新 更多