duan19740319 2015-07-28 12:22
浏览 55
已采纳

如何使用javascript动态命名表单元素

I'm scripting an html form inside a PHP. The form is shown as a table. Every row is a 'set' of passed items that will be gathered in a $_POST double array where the first id is the table ROW, and the second identifier is the COLUMNS, actually the variables of the form.

Here is an example.

<?php
echo '<form><table>';
for ($i=1;$i<10;$i++){
echo '
   <tr>
       <td><input type="text" name="data[',$i,'][element1]></td>
       <td><input type="text" name="data[',$i,'][element2]></td>
       <td><input type="text" name="data[',$i,'][element3]></td>
       ...
       <td><input type="text" name="data[',$i,'][elementN]></td>
   </tr>';
}
echo '</table></form>';
?>

The "gathering" part of the script simply is:

$myFormInformation = $_REQUEST['data'];

and shows the content like this:

data[1][element1] = element 1-1
data[1][element2] = element 1-2
...
data[1][elementN] = element 1-N
..
data[2][element1] = element 2-1
...
data[2][elementN] = element 2-N
... ... ... 
data[10][elementN] = element 10-N

I rendered the idea...and it perfectly works. However I don't want to have all teh 10 rows shown for input, but, instead, grant the user to add rows if needed.

I found this javascript snippet to add rows, however the problem is that I don't know how to pass the ROW ID dynamically to allow my $_REQUEST part of code to work. At this moment in fact the $_REQUEST gets only one row.

<SCRIPT language="javascript">
    function addRow() {
        var referenceNodes = document.getElementById("flightsTable").getElementsByTagName("tr");
            //-2 because last row is button row
            var referenceNode = referenceNodes[referenceNodes.length - 1];
            var newNode = referenceNode.cloneNode(true);
            referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
        }
        document.getElementById("addNewRow").onclick = addRow;
</script>

obviously there is a button with id=addNewRow to launch the javascript.

How can I pass a "counter" from the javascript to the forms element in order to have each new row increase it's name="data[i][elementX]".

I can accept other ways to do the job done, if easier...

thanks!

  • 写回答

1条回答 默认 最新

  • dpfl37651 2015-07-28 12:43
    关注

    You can loop the td elements and then find the input. Setting the name attribute as you please.

    Something like this:

    function addRow() {
        var referenceNodes = document.getElementById("flightsTable").getElementsByTagName("tr");
        //-2 because last row is button row
        var referenceNode = referenceNodes[referenceNodes.length - 1];
        var newNode = referenceNode.cloneNode(true);
    
        // get the next count for the new row.
        var dataCount = referenceNodes.length + 1;
    
        // loop the cells.
        var cells = newNode.getElementsByTagName("td");
        for (var i = 0; i < cells.length; i++) {
            var cell = cells[i];
    
            // get the input (assuming only one).
            var input = cell.getElementsByTagName("input")[0];
    
            // set the name property.
            input.name = "data['" + dataCount + "'][element" + (i + 1) + "]";
        }
    
        referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
    }
    

    Here is a working example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值