【问题标题】:jQuery select specific optgroup and remove optionsjQuery 选择特定的 optgroup 并删除选项
【发布时间】:2013-09-23 12:42:50
【问题描述】:

如何删除 select 的特定 optgroup 的选项

到目前为止我所做的是:

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 添加新选项

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

注意:testers_team-${info.problemId}的值和我上面贴的select是同一类

【问题讨论】:

  • $('.'+id) 中“id”的值是多少?
  • $('.'+id) 是代码中下拉列表的类,简而言之,它与我在上面发布的select 中的class="testers_team-${info.problemId}" 具有相同的值

标签: jquery


【解决方案1】:

您正在查找、清空并附加到选择。选择 optgroup 并改为使用它:

function (data) {
    $('.' + id).each(function () {
        var $optGroup = $(this).find('optgroup.test_list');
        $optGroup.empty();
        $.each(data, function (index, value) {
            $('<option>').text(value).appendTo($optGroup);
        });
    });
}

【讨论】:

  • 它就像一个魅力,在我接受你的回答之前你能解释一下细节吗?
  • 它被解释了......它找到,它清空然后它附加选项元素......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多