在jsp页面里设置一张表
最开开始的时候显示一行空的
在假设你输入就敲回车显示下一行这样怎么写
最好给个实例.
  

解决方案 »

  1.   


    <table id="testTable" border="1">
    <tr><td>&nbsp;</td></tr>
    </table>
    <input type="button" value="Add" onclick="addRow()"/><script>
    function addRow() {
    var table = document.getElementById("testTable");
    var tbody = table.tBodies[0];
    var tr = document.createElement("TR");
    var td = document.createElement("TD");
    td.innerHTML = "新行";
    tr.appendChild(td);
    tbody.appendChild(tr);
    table.appendChild(tbody);
    }
    </script>
      

  2.   

    这样Add的TEXT的值都是"新行"了
    我想要的就是可以在text框可输入的
    一行4个
    在一个怎么去接收他的值..
      

  3.   


    <table id="testTable" border="1">
    <tr><td>&nbsp;</td></tr>
    </table>
    <input type="button" value="Add" onclick="addRow()"/>
    <input type="button" value="Get Value" onclick="getValue()"/><script>
    var textIndex = 0;
    function addRow() {
    var table = document.getElementById("testTable");
    var tbody = table.tBodies[0];
    var tr = document.createElement("TR");var td = document.createElement("TD");
    var input = document.createElement("INPUT");
    input.type="text";
    input.id="testInput" + textIndex;
    td.appendChild(input);
    tr.appendChild(td);
    textIndex++;td = document.createElement("TD");
    input = document.createElement("INPUT");
    input.type="text";
    input.id="testInput" + textIndex;
    td.appendChild(input);
    tr.appendChild(td);
    textIndex++;td = document.createElement("TD");
    input = document.createElement("INPUT");
    input.type="text";
    input.id="testInput" + textIndex;
    td.appendChild(input);
    tr.appendChild(td);
    textIndex++;td = document.createElement("TD");
    input = document.createElement("INPUT");
    input.type="text";
    input.id="testInput" + textIndex;
    td.appendChild(input);
    tr.appendChild(td);
    textIndex++;tbody.appendChild(tr);
    table.appendChild(tbody);
    }function getValue() {
    for(var i = 0; i < textIndex; i++) {
    var text = document.getElementById("testInput" + i);
    alert(text.value);
    }
    }
    </script>
      

  4.   

    兄弟帮忙帮到底 真是急
      最好能不能不用ADD!直接用回车键!
       那个接收值给个变量接收就好了,要get vlaue 因为值之后是要存到数据库去的
       帮下我谢了
      

  5.   


    把ADD的事件,放在回车按钮上就好啦^_^
      

  6.   

    回车是指在text文本框里回车就生成第二行
    值是怎么在JSP页码取(就是每一行的4个TEXT文本框的值!!!之后是把这一行的值存在数据库做一条记录..)
      

  7.   


    <table id="testTable" border="1">
    <tr id="testTr0">
    <td><input type="text" id="testInput0" value="" onkeydown="addRow('1')"/></td>
    <td><input type="text" id="testInput1" value="" onkeydown="addRow('1')"/></td>
    <td><input type="text" id="testInput2" value="" onkeydown="addRow('1')"/></td>
    <td><input type="text" id="testInput3" value="" onkeydown="addRow('1')"/></td>
    </tr>
    </table>获得第<input type="text" id="getRowValueIndex" value=""/>行的值
    <input type="button" value="Get Value" onclick="getValue()"/><script>
    var textIndex = 4;
    var rowIndex = 1;
    function addRow(newRowIndex) {
    if (event.keyCode == 13 && newRowIndex == rowIndex) {
    var table = document.getElementById("testTable");
    var tbody = table.tBodies[0];
    var tr = document.createElement("TR");
    tr.id="testTr" + rowIndex;
    rowIndex++; var td;
    var input;
    for (var i = 0; i < 4; i++) {
    td = document.createElement("TD");
    input = document.createElement("INPUT");
    input.type="text";
    input.id="testInput" + textIndex;
    input.onkeydown = function () {addRow(rowIndex);};
    td.appendChild(input);
    tr.appendChild(td);
    textIndex++;
    }
    tbody.appendChild(tr);
    table.appendChild(tbody);
    }
    }

    function getValue() {
    var getRowValueIndex = document.getElementById("getRowValueIndex");
    var startInputIndex = getRowValueIndex.value * 4 - 4;
    var endInputIndex = getRowValueIndex.value * 4;
    if (getRowValueIndex.value != "") {
    var input1 = document.getElementById("testInput" + (getRowValueIndex.value * 4 - 4)).value;
    var input2 = document.getElementById("testInput" + (getRowValueIndex.value * 4 - 3)).value;
    var input3 = document.getElementById("testInput" + (getRowValueIndex.value * 4 - 2)).value;
    var input4 = document.getElementById("testInput" + (getRowValueIndex.value * 4 - 1)).value;
    alert("第 " + getRowValueIndex.value + " 行,第1个文本框的值为:" + input1 + ";第2个文本框的值为:" + input2 + ";第3个文本框的值为:" + input3 + ";第4个文本框的值为:" + input4);
    }
    }
    </script>