Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用循环在选择列表中动态更改ID_Javascript_Php_Html_Select_For Loop - Fatal编程技术网

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

Javascript 使用循环在选择列表中动态更改ID,javascript,php,html,select,for-loop,Javascript,Php,Html,Select,For Loop,我目前有一个表单,可以使用javascript在单击时添加新条目。但是,我想更改每个后续加载项的id: e、 g.第一个主体的id为participant1,但下一个主体的id为participant2,第八个主体的id为participant8,依此类推 以下是主文件: <body> <form action="process" class="register" method="POST"> <h1>Add Participa

我目前有一个表单,可以使用javascript在单击时添加新条目。但是,我想更改每个后续加载项的id: e、 g.第一个主体的id为participant1,但下一个主体的id为participant2,第八个主体的id为participant8,依此类推

以下是主文件:

<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>
下面是JS函数:

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?");

}
}
HTML代码

<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>