在列表框中获取所选值的列表 [英] Get a List of the Selected Values in a ListBox

查看:117
本文介绍了在列表框中获取所选值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

此刻,我想我将使用其他问题的大部分答案,但最后不要使用.text(),然后遍历比赛.更好的主意?

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?

推荐答案

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

You can take multiple selected text by 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 value then simply write.

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

演示

Demo

这篇关于在列表框中获取所选值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆