使用JS向表列添加select下拉列表

时间:2016-02-06 05:58:23

标签: javascript

我用HTML创建了一个表(4行3列)。但我想在第二列中添加一个选择下拉列表。我无法在列中添加。

我的代码是:

<!DOCTYPE html>
<html>
<body>

<table border="1" style="width:100%">
  <tr>
    <th>Criteria</th>
    <th>Yes/No</th> 
    <th>Comments</th>
  </tr>
<script>

var array1 = [1,2,3,4];
for(var i=0; i<array1.length; i++)
{ 

document.write("<tr>");

document.write("<td>"+array1[i]+"</td>");
document.write("<td>  </td>");
document.write("<td>  </td>");

document.write("</tr>");


}
</script>
</table>
</body>
</html>

选择下拉HTML是:

<select name="mydropdown">
    <option value="Milk">Fresh Milk</option>
    <option value="Cheese">Old Cheese</option>
    <option value="Bread">Hot Bread</option>
</select>

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助

<!DOCTYPE html>
<html>

<body>

  <table border="1" style="width:100%">
    <tr>
      <th>Criteria</th>
      <th>Yes/No</th>
      <th>Comments</th>
    </tr>
    <script>
      var array1 = [1, 2, 3, 4];
      for (var i = 0; i < array1.length; i++) {

        var dropdown = "<select name='mydropdown'>" +
          "<option value='Milk'>Yes</option>" +
          "<option value='Cheese'>No</option>" +
          "</select>";

        document.write("<tr>");

        document.write("<td>" + array1[i] + "</td>");
        document.write("<td>" + dropdown + "</td>");
        document.write("<td>  </td>");

        document.write("</tr>");


      }
    </script>
  </table>
</body>

</html>

相关问题