这是一个简单的javascript add row与函数,然后我不知道如何限制额外的行,例如最大。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>提前感谢
发布于 2010-11-02 13:01:19
在添加前先计算行数。行只是表元素中的一个数组-您可以通过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>发布于 2011-11-02 03:32:50
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++;
}
}https://stackoverflow.com/questions/4074998
复制相似问题