如何在select2 JavaScript multiselect中选择所有选项 [英] How to select all options in select2 JavaScript multiselect
本文介绍了如何在select2 JavaScript multiselect中选择所有选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
前几天,我试图弄清楚如何在select2 v3.5.1 JavaScript multiselect控件中选择所有项.我尝试了一些尝试,但是我很难弄清楚该怎么做.我只是想选择框中的每个选项,但是显然select2没有内置选项可以为您选择所有项目.
The other day, I was trying to figure out how to select all items in select2 v3.5.1 JavaScript multiselect control. I tried a few things, but I was having a difficult time figuring out how to do it. I just wanted to select every option in the box, but apparently select2 does not have a built-in option to select all of the items for you.
推荐答案
以下是OP答案的更有效版本:
Here's a slightly more efficient version of the OP's answer:
var selectedItems = [];
var allOptions = $("#IncludeFieldsMulti option");
allOptions.each(function() {
selectedItems.push( $(this).val() );
});
$("#IncludeFieldsMulti").select2("val", selectedItems);
或者使其更简洁:
var selectedItems = $('#IncludeFieldsMulti option').map(function() { return this.value });
$("#IncludeFieldsMulti").select2("val", selectedItems);
这篇关于如何在select2 JavaScript multiselect中选择所有选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文