jQuery 获取列表框中选定值的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28424067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:28:23  来源:igfitidea点击:

Get a List of the Selected Values in a ListBox

jquerylistbox

提问by DCShannon

There's already a question with a ton of votes for getting the selected value from a dropdown using jQuery here.

已经有一个关于使用 jQuery here从下拉列表中获取所选值的问题

That answer almost works for a listbox, but if multiple values are selected, the result is a single string with all the values concatenated. This is not useful. I need a collection (list, array, whatever) of the text values for each selected option.

该答案几乎适用于列表框,但如果选择了多个值,则结果是将所有值连接在一起的单个字符串。这没有用。我需要每个选定选项的文本值的集合(列表、数组等)。

At the moment I'm thinking I'll use most of the answer from that other question, but without the .text()at the end, and then iterate through the matches. Better ideas?

目前我想我会使用其他问题的大部分答案,但最后没有.text(),然后遍历匹配项。更好的想法?

回答by Sadikhasan

You can take multiple selected textby iterating loop as mentioned below.

您可以通过如下所述的迭代循环来获取多个选定的文本

$('#f1').click(function(){    
   var rr = []; 
   $('.selectpicker :selected').each(function(i, selected){ 
        rr[i] = $(selected).text(); 
    });
    alert(rr);
});

OR if you want to using it's valuethen simply write.

或者,如果您想使用它的价值,那么只需编写即可。

$('.selectpicker').val();

Demo

演示

回答by Butani Vijay

You can use as below :

您可以使用如下:

var selectedVal= []; 
$('#multiple :selected').each(function(i, selected){ 
   selectedVal[i] = $(selected).text(); 
   alert(selectedVal[i]);
});

Here is Tutorial and example

这是教程和示例