0

我有一个表格,其中包含选择下拉列表和剩余位置的值,如 html 所示:

<table class="day-choices">
    <thead>
        <tr>
            <th>Time</th>
            <th class="day-choices-pleft">Places Left</th>
            <th class="day-choices-preq">Places Req.</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>10.30</td>
            <td class="day-choices-pleft">6</td>
            <td class="day-choices-preq">
                <select name="places-req[]" class="places-req">

                </select>
            </td>
        </tr>
        <tr>
            <td>11.30</td>
            <td class="day-choices-pleft">8</td>
            <td class="day-choices-preq">
                <select name="places-req[]" class="places-req">
                    <option value="">0</option>

                </select>
            </td>
        </tr>
        <tr>
            <td>12.30</td>
            <td class="day-choices-pleft">10</td>
            <td class="day-choices-preq">
                <select name="places-req[]" class="places-req">
                    <option value="">0</option>

                </select>
            </td>
        </tr>
        <tr>
            <td>13.30</td>
            <td class="day-choices-pleft">5</td>
            <td class="day-choices-preq">
                <select name="places-req[]" class="places-req">
                    <option value="">0</option>

                </select>
            </td>
        </tr>
        <tr>
            <td>14.30</td>
            <td class="day-choices-pleft">6</td>
            <td class="day-choices-preq">
                <select name="places-req[]" class="places-req">
                    <option value="">0</option>

                </select>
            </td>
        </tr>
    </tbody>
</table>

我想做的是根据td.day-choices-pleft使用 jquery 的值填充选择选项。

例如,如果在一个表格行中,我有 6 个位置 id 像一个 select 一样有 6 个选项(包括 0 选项):

<select name="places-req[]" class="places-req">
    <option value="">0</option>
    <option value="">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
</select>
4

1 回答 1

1
$('td.day-choices-pleft').each(function(){
  var val = parseInt($(this).text());
  var $select = $(this).next('td.day-choices-preq').find('select');
  $select.empty();
  for(var i = 0 ; i <= val ; i++){
     $select.append("<option value="+i+">"+i+"</option>");
  }
});
于 2013-05-09T14:24:48.573 回答