簡體   English   中英

添加選項以在每個表行中選擇

[英]Add option to select in each table row

我在每個表行中都有一個帶有多個“選擇”的表。

我需要遍歷每個表行,並將“選項”附加到數組的選擇中。 當前,只有表中的第一個“選擇”填充了數組中的數據。

我有以下HTML:

<table id="myTable">
<thead>
    <tr>
        <th>Code</th>
        <th>Date</th>
        <th>Description</th>
        <th>Percentage</th>
    </tr>
</thead>
<tbody>
    <tr class="RepeatingSection">
        <td>
            <select id="InputCode"></select>
        </td>
        <td>
            <input type="date" name="Date" id="DateInput" required="required" />
        </td>
        <td>
            <input type="text" id="DetailsInput" name="Details" size="35" required="required" />
        </td>
        <td>
            <input type="number" id="percentageInput" max="100" value="100" />%</td>
    </tr>
    <tr class="RepeatingSection">
        <td>
            <select id="InputCode"></select>
        </td>
        <td>
            <input type="date" name="Date" id="DateInput" required="required" />
        </td>
        <td>
            <input type="text" id="DetailsInput" name="Details" size="35" required="required" />
        </td>
        <td>
            <input type="number" id="percentageInput" max="100" value="100" />%</td>
    </tr>
    <tr class="RepeatingSection">
        <td>
            <select id="InputCode"></select>
        </td>
        <td>
            <input type="date" name="Date" id="DateInput" required="required" />
        </td>
        <td>
            <input type="text" id="DetailsInput" name="Details" size="35" required="required" />
        </td>
        <td>
            <input type="number" id="percentageInput" max="100" value="100" />%</td>
    </tr>
</tbody>

我目前有以下JavaScript:

var array;
var i;
var option;
var select;

array = [001, 002, 003, 004];

for (i = 0; i < array.length; i++) {
  option = document.createElement("option");
  option.value = array[i];
  option.text = array[i];
  select = document.getElementById("InputCode");

  $('#myTable .RepeatingSection').each(function() {
    $(this).find('#InputCode').each(function() {
      select.appendChild(option);
    })
  })
  select.appendChild(option);
}

也請看我的小提琴

任何幫助,將不勝感激。 希望這篇文章有意義。

嘗試以下JavaScript代碼:

var array;
var i;
var option;

array = [001, 002, 003, 004];

for (i = 0; i < array.length; i++) {
    option = document.createElement("option");
    option.value = array[i];
    option.text = array[i];

    $('#myTable .RepeatingSection').find( "select[id=InputCode]" ).append(option);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM