添加更多输入时,如何更改输入文本的名称

时间:2015-04-01 12:12:44

标签: javascript php jquery forms

这是表格

<form action=""  method="POST">
  <input type="button" value="addmore" onClick="addRow('dataTable')" />
  <table id="dataTable" class="TFtable" border="1">
    <tr>
      <td><label>should be auto increment(like (name1, name2, name3 ...))</label>
        <input type="text" required="required" name="DONINID[]" value=""></td>
    </tr>
  </table>
  <input class="submit" type="submit" value="Confirm &raquo;" />
</form>

这是添加更多的脚本=&gt;的script.js:

function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    if(rowCount < 20){                          // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        }
    }else{
         alert("Maximum Passenger per ticket is 20.");

    }
}

function deleteRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {                         // limit the user from removing all the fields
                alert("Cannot Remove all the Passenger.");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
}

现在idia是如何改变标签名称的,因为我们添加了更多输入框,它应该是自动增量,如:

name1,name2,name3,name3以及我们添加它时应该向我们展示。

提前感谢。

1 个答案:

答案 0 :(得分:0)

添加一个跟踪下一个#的变量,并在每次添加时将其附加到内容和名称。

<强> HTML

<form action="" method="POST">
<input type="button" value="addmore" onClick="addRow('dataTable')" />
<table id="dataTable" class="TFtable" border="1">
    <tr>
        <td>
            <label>Name 1:</label>
            <input type="text" required="required" name="name1" />
        </td>
    </tr>
</table>
<input class="submit" type="submit" value="Confirm &raquo;" />

<强>的JavaScript

var nextName = 2;

function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    if (rowCount < 20) { // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for (var i = 0; i < colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = '<label>Name ' + nextName + ':</label>' +
                '<input type="text" required="required" name="name' + nextName + '" />';
            nextName++;
        }
    } else {
        alert("Maximum Passenger per ticket is 20.");

    }
}

function deleteRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    for (var i = 0; i < rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if (null != chkbox && true == chkbox.checked) {
            if (rowCount <= 1) { // limit the user from removing all the fields
                alert("Cannot Remove all the Passenger.");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }
    }
}

演示http://jsfiddle.net/BenjaminRay/4jnw0t09/