如何使用 javascript 或 jQuery 快速选择预设的多选列表框项目?

发布于 2024-08-19 13:21:42 字数 200 浏览 2 评论 0原文

假设我的页面有一个索引列表“1,3,4,5,9,12”和一个包含 12 个项目的多选列表框。

使用 javascript 告诉列表框多选这些索引处的所有项目的快速方法是什么?

使用 jQuery 如何做到这一点?

因此,例如,如果用户选择与“candybar”列表框关联的“焦糖”预设,它将选择所有具有焦糖的糖果......我想您明白了。

So say my page has a list of indexes '1,3,4,5,9,12' and a multi-select listbox with 12 items in it.

What's a fast way to use javascript to tell the listbox to multi-select all items at those indexes?

How would this be done using jQuery?

So for example if the user selects the 'caramel' preset associated with the 'candybar' listbox, it will select all the candy bars that have caramel... I think you get the idea.

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

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

发布评论

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

评论(3

孤者何惧 2024-08-26 13:21:42

这可以解决问题:

<select id="select" multiple="multiple">
    <option value="1">test 1</option>
    <option value="2">test 2</option>
    <option value="3">test 3</option>
    <option value="4">test 4</option>
    <option value="5">test 5</option>
    <option value="6">test 6</option>
    <option value="7">test 7</option>
    <option value="8">test 8</option>
    <option value="9">test 9</option>
    <option value="10">test 10</option>
    <option value="11">test 11</option>
    <option value="12">test 12</option>
</select>

Javascript (jQuery):

indexes = [1,3,4,5,9,12]
$(document).ready(function(){
    for(i=0; i<indexes.length; i++){
        $('#select option:eq(' + (indexes[i]-1) + ')').attr('selected', 'selected');
    }
});

Without jQuery:

window.onload = function(){
    var indexes = [1,3,4,5,9,12];
    var options = document.getElementById('select').options;
    for(i=0; i<indexes.length; i++){
        options[indexes[i]-1].selected = true;
    }
}

This could do the trick:

<select id="select" multiple="multiple">
    <option value="1">test 1</option>
    <option value="2">test 2</option>
    <option value="3">test 3</option>
    <option value="4">test 4</option>
    <option value="5">test 5</option>
    <option value="6">test 6</option>
    <option value="7">test 7</option>
    <option value="8">test 8</option>
    <option value="9">test 9</option>
    <option value="10">test 10</option>
    <option value="11">test 11</option>
    <option value="12">test 12</option>
</select>

Javascript (jQuery):

indexes = [1,3,4,5,9,12]
$(document).ready(function(){
    for(i=0; i<indexes.length; i++){
        $('#select option:eq(' + (indexes[i]-1) + ')').attr('selected', 'selected');
    }
});

Without jQuery:

window.onload = function(){
    var indexes = [1,3,4,5,9,12];
    var options = document.getElementById('select').options;
    for(i=0; i<indexes.length; i++){
        options[indexes[i]-1].selected = true;
    }
}
百合的盛世恋 2024-08-26 13:21:42

jquery select 插件 有一个 selectOptions(value[, clear ]) 方法,在选择框中选择多个值。但它使用值作为参数而不是索引。

The jquery select plugin has a selectOptions(value[, clear]) method which selects multiple values in a select box. But it takes the values as parameter instead of indexes.

不弃不离 2024-08-26 13:21:42

您最好在选项元素上设置类并以这种方式寻址它们,而不是通过索引:

<select id="my-select">
  <option class="caramel">Twix</option>
  <option>Mounds</option>
  <option class="caramel">Milky Way</option>
  <!-- ... -->
</select>

然后:

$("option.caramel", "#my-select").each(function () { this.selected = true });

编辑:

但是如果您真的想通过索引来完成它,您可以这样做:(

function selectOptionsByIndex(select, indexes) {
    var i = 0;
    select.children().each(function (j) { if (indexes[i] == j) { ++i; this.selected = true } });
}

selectOptionsByIndex($("#my-select"), [ 1, 3, 4, 5, 9, 12 ]);

这取决于列表提供的索引按升序排列。)

You'd be better off setting classes on the option elements and addressing them that way, rather than by index:

<select id="my-select">
  <option class="caramel">Twix</option>
  <option>Mounds</option>
  <option class="caramel">Milky Way</option>
  <!-- ... -->
</select>

And then:

$("option.caramel", "#my-select").each(function () { this.selected = true });

Edit:

But if you really want to do it by index, you could do:

function selectOptionsByIndex(select, indexes) {
    var i = 0;
    select.children().each(function (j) { if (indexes[i] == j) { ++i; this.selected = true } });
}

selectOptionsByIndex($("#my-select"), [ 1, 3, 4, 5, 9, 12 ]);

(This depends on the list of supplied indexes being in ascending order.)

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