doubai9014 2015-05-11 22:36
浏览 94
已采纳

使用循环在选择列表中动态更改ID

I currently have a form that can add new entries on click using javascript. However, I would like to change the id of each subsequent "add-on": e.g. The first tbody has an id of "participant1", but the next one will have an id of "participant2", the eighth "participant8", and so forth.

Here is the main file:

<body>    
    <form action="process" class="register" method="POST">
        <h1>Add Participants</h1>

        <fieldset class="row2">
            <legend>List of Participants</legend>
            <p> 
                <input type="button" value="Add Participant" onClick="addRow('dataTable')" /> 
                <input type="button" value="Clear Participants" onClick="deleteRow('dataTable')"  /> 
                <p>(All acions apply only to entries with check marked check boxes only.)</p>
            </p>
           <table id="dataTable" class="form" border="1">
              <tbody>
                <tr>
                  <p>
                    <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
                    <td>
                        <div>Participant: </div>
                        <select name="participant1" id="participant1">
                            <option>Person A</option>
                        <option>Person B</option>
                        <option>Person C</option>
                            </select>
                     </td>
          </p>
                </tr>
                </tbody>
            </table>
            <div class="clear"></div>
        </fieldset>


        <input class="submit" type="submit" value="Confirm &raquo;" />
        <div class="clear"></div>
    </form>

</body>

And here is the JS Function:

function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 50){                          
    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("More than 50 Participants? Are you sure?");

}
}
  • 写回答

1条回答 默认 最新

  • duancong2965 2015-05-11 23:08
    关注

    HTML Code

    <html>
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <title>test</title>
    </head>
    <body>  
          <form action="process" class="register" method="POST">
              <h1>Add Participants</h1>
    
              <fieldset class="row2">
                  <legend>List of Participants</legend>
                  <p> 
                      <input type="button" value="Add Participant" onClick="addRow('dataTable')" /> 
                      <input type="button" value="Clear Participants" onClick="deleteRow('dataTable')"  /> 
                      <p>(All acions apply only to entries with check marked check boxes only.)</p>
                  </p>
                 <table id="dataTable" class="form" border="1">
                    <tbody>
                      <tr>
                        <p>
                          <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
                          <td>
                              <div>Participant: </div>
                              <select name="participant1" id="participant1">
                                  <option>Person A</option>
                              <option>Person B</option>
                              <option>Person C</option>
                                  </select>
                           </td>
                </p>
                      </tr>
                      </tbody>
                  </table>
                  <div class="clear"></div>
              </fieldset>
    
    
              <input class="submit" type="submit" value="Confirm &raquo;" />
              <div class="clear"></div>
          </form>
    
          <script>
          var j=1;
          function addRow(tableID) {
          var table = document.getElementById(tableID);
          var rowCount = table.rows.length;
          if(rowCount < 50){                          
              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.id="participant"+ j;
                  newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                  j++;
              }
          }else{
               alert("More than 50 Participants? Are you sure?");
    
          }
          }
    
          </script>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导