在选择框中快速选择整个 optgroup 的简单方法

发布于 2024-08-05 22:36:36 字数 85 浏览 4 评论 0原文

我有一个选择框,可以在其中选择多个选项。选择框中有多个 optgroup。 有没有一种简单的方法可以在javascript中一次选择整个optgroup?

I have a select box in which i can select multiple options. In the select box are multiple optgroups.
Is there an easy way to select a whole optgroup at once in javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聆听风音 2024-08-12 22:36:36

我建议使用 jQuery(或其他框架)来快速处理 DOM 选择。给每个 optgroup 一个类,以便更容易获取它。

$("optgroup.className").children().attr('selected','selected');

如果您想根据用户选择组来选择整个组,请执行以下操作:

$("optgroup.className").select(function(e) {
  $(this).children().attr('selected','selected');
});

**两个示例都是未经测试的伪代码,但如有必要,它们应该只需进行最少的更改即可工作。

如果您无法使用框架,则必须自己遍历 DOM 来查找 optgroup 和子项。您可以将侦听器附加到 select 元素以获取所选元素,然后也以这种方式遍历到子元素。

I suggest using jQuery (or another framework) to quickly handle DOM selections. Give each optgroup a class to make it easier to grab it.

$("optgroup.className").children().attr('selected','selected');

If you want to select the entire group based on the user selecting the group, do the following:

$("optgroup.className").select(function(e) {
  $(this).children().attr('selected','selected');
});

**Both examples are untested pseudo-code, but they should work with minimal changes, if necessary.

If you cannot use a framework, you'll have to traverse the DOM yourself to find the optgroup and children. You could attach a listener to the select element to grab the element being selected then traverse to the children that way, too.

懷念過去 2024-08-12 22:36:36

我通常反对使用 jQuery 来完成这样的简单工作,但我可以在这里看到它的价值。不过,如果您更喜欢非 jQuery 解决方案,该解决方案具有不使用库、不引入虚假 id 或类并且运行速度更快的优点,这里有一个:

<script type="text/javascript">

function selectOptGroupOptions(optGroup, selected) {
    var options = optGroup.getElementsByTagName("option");
    for (var i = 0, len = options.length; i < len; i++) {
        options[i].selected = selected;
    }
}

function selectOptGroup(selectId, label, selected) {
    var selectElement = document.getElementById(selectId);
    var optGroups = selectElement.getElementsByTagName("optgroup");
    var i, len, optGroup;
    for (i = 0, len = optGroups.length; i < len; i++) {
        optGroup = optGroups[i];
        if (optGroup.label === label) {
            selectOptGroupOptions(optGroup, selected);
            return;
        }
    }
}

</select>

<select id="veg" multiple>
    <optgroup label="roots">
        <option>Swede</option>
        <option>Carrot</option>
        <option>Turnip</option>
    </optgroup>
    <optgroup label="leaves">
        <option>Spinach</option>
        <option>Kale</option>
    </optgroup>
</select>

<input type="button" onclick="selectOptGroup('veg', 'roots', true)" value="Select roots">

如果您的 有一个 id您可以取消 selectOptGroup 函数,并将 optgroup 直接传递到 selectOptGroupOptions 中。

I'm normally against using jQuery for simple jobs like this but I can see its value here. Still, if you prefer a non-jQuery solution that will have the benefits of using no library, introducing no spurious ids or classes and running faster, here is one:

<script type="text/javascript">

function selectOptGroupOptions(optGroup, selected) {
    var options = optGroup.getElementsByTagName("option");
    for (var i = 0, len = options.length; i < len; i++) {
        options[i].selected = selected;
    }
}

function selectOptGroup(selectId, label, selected) {
    var selectElement = document.getElementById(selectId);
    var optGroups = selectElement.getElementsByTagName("optgroup");
    var i, len, optGroup;
    for (i = 0, len = optGroups.length; i < len; i++) {
        optGroup = optGroups[i];
        if (optGroup.label === label) {
            selectOptGroupOptions(optGroup, selected);
            return;
        }
    }
}

</select>

<select id="veg" multiple>
    <optgroup label="roots">
        <option>Swede</option>
        <option>Carrot</option>
        <option>Turnip</option>
    </optgroup>
    <optgroup label="leaves">
        <option>Spinach</option>
        <option>Kale</option>
    </optgroup>
</select>

<input type="button" onclick="selectOptGroup('veg', 'roots', true)" value="Select roots">

If your <optgroup> has an id you could do away with the selectOptGroup function and just pass the optgroup straight into selectOptGroupOptions.

乖乖 2024-08-12 22:36:36

jQuery:

$('#myoptgroup option').attr('selected', true);

jquery:

$('#myoptgroup option').attr('selected', true);
陌生 2024-08-12 22:36:36

我刚才正在尝试做类似的事情。

我想在单击组的标签时选择 。第一次尝试是这样的:

$('select > optgroup').click(function () {
    $(this).children().attr('selected', true);
});

这个解决方案成功了一半...

单击 的标签后,它的所有子项都被选中。

但是当简单地单击时,它仍然选择组中的所有其他!问题是事件冒泡,因为 位于 内部,从技术上讲,您也单击了它。

因此,最后一个难题是在实际单击 的情况下抑制事件向上冒泡。最终的解决方案是:

$('select > optgroup').click(function () {
    $(this).children().attr('selected', true);
});

$('select > optgroup > option').click(function (e) {
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
});

工作完成!

编辑

令人愤怒的是,这在 IE8 中不起作用(并且可疑

它决定完全忽略 和 元素上的点击事件。我能想到的唯一选择是将元素放置在 optgroup 标签上方以捕获点击,但它可能不值得付出努力......

I was trying to do something similar just now.

I wanted to select an <optgroup>'s <option>s upon clicking the group's label. The first attempt went like this:

$('select > optgroup').click(function () {
    $(this).children().attr('selected', true);
});

This solution half worked...

Upon clicking the <optgroup>'s label all of its children became selected.

BUT when simply clicking an <option> it was still selecting all the other <option>s in the group! The problem was event bubbling, because the <option> is inside the <optgroup> technically you're clicking it too.

Therefore the final piece of the puzzle was to suppress the event bubbling upwards in the event that an <option> was actually clicked instead. The final solution then became:

$('select > optgroup').click(function () {
    $(this).children().attr('selected', true);
});

$('select > optgroup > option').click(function (e) {
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
});

Job done!

EDIT

Infuriatingly this doesn't work work in IE8 (and doubtful < IE8 - maybe IE9?)...

It decides to totally ignore click events on both and elements. The only alternative that I can think of is to position elements above the optgroup labels to capture the click, but its probably not worth the effort...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文