1

如何删除特定optgroup的选项select

到目前为止我所做的是:

function(data)
{
    $('.'+id).each(function()
    {
        $(this).find('option').remove();
        $.each(data, function(index, value) {              
        $('<option>').text(value).appendTo('.'+id);
        });
    });

}

而且select

<select id="testers" class="testers_team-${info.problemId}">
    <optgroup label="Current Assigned Tester">
        <option>I'll think about this later</option>
    </optgroup>
    <optgroup label="Testers" class="test_list">
        <c:forEach var="testers_per_team" items='${test.KwekKwek}'>
            <option>${testers_per_team}</option>
        </c:forEach>
    </optgroup>
</select>

我想删除第二个选项optgroup class="test_list"并重新填充它,我成功添加了选项,但不是具体optgroup我想知道如何:

  1. 使用具有多个 optgroup 的 select 选择特定 optgroup
  2. 删除当前添加到该选项的选项
  3. 向当前选择的 optgroup 添加新选项

上面的当前脚本在 an 下,Ajax但它删除了下拉列表的所有选项,所以请帮忙。

注意:的值与上面发布testers_team-${info.problemId}selecti相同

4

1 回答 1

1

You're finding, emptying and appending to the select. Select the optgroup and work with that instead:

function (data) {
    $('.' + id).each(function () {
        var $optGroup = $(this).find('optgroup.test_list');
        $optGroup.empty();
        $.each(data, function (index, value) {
            $('<option>').text(value).appendTo($optGroup);
        });
    });
}
于 2013-09-23T12:51:14.487 回答