如何使用 jquery 遍历多个选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1227684/
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 10:57:40  来源:igfitidea点击:

how to iterate through multiple select options with jquery

jquery

提问by akano1

I was just wondering if it's possible to go through multiple select options and get their values and text(if one is selected get the value and text, if 2 is selected get both of their values and text and so on)

我只是想知道是否可以通过多个选择选项并获取它们的值和文本(如果选择一个获取值和文本,如果选择 2 获取它们的值和文本等等)

I have 15 select boxes in one page?

我在一页中有 15 个选择框?

any help would be appreciated.

任何帮助,将不胜感激。

 <form>
        <select class="select" name="select3" id="select3">
          <option value="0">0</option>
          <option value="1.99">1</option>
          <option value="1.99">2</option>
          <option value="1.99">3</option>
          <option value="1.99">4</option>
          <option value="1.99">5</option>
          <option value="1.99">6</option>
          <option value="1.99">7</option>
          <option value="1.99">8</option>
        </select>
        </form>

  <form> 
      <select  class="select" name="select" id="select">
            <option value="0">0</option>
            <option value="1.99">1</option>
            <option value="1.99">2</option>
            <option value="1.99">3</option>
            <option value="1.99">4</option>
            <option value="1.99">5</option>
            <option value="1.99">6</option>
            <option value="1.99">7</option>
            <option value="1.99">8</option>
          </select>
   </form>

all the select options have the same class.

所有选择选项都具有相同的类。

thanks

谢谢

回答by karim79

This will alert all the selected options' text and values (for all selects on the page):

这将提醒所有选定选项的文本和值(对于页面上的所有选择):

$('select > option:selected').each(function() {
    alert($(this).text() + ' ' + $(this).val());
});

See Core/each and Selectors/selected:

参见 Core/each 和 Selectors/selected:

http://docs.jquery.com/Core/each

http://docs.jquery.com/Core/each

http://docs.jquery.com/Selectors/selected

http://docs.jquery.com/Selectors/selected

回答by montana

for optgroups...

对于选择组...

$("select[id^='desu']").children('optgroup').children('option:selected').each( 
    function(id, element) {
        document.write(element.title);
    }
);

回答by tvanfosson

This function will return an array of text/value pairs for the selects matching the given class.

此函数将为匹配给定类的选择返回一个文本/值对数组。

function getSelects(klass) {
    var selected = [];
    $('select.' + klass).children('option:selected').each( function() {
        var $this = $(this);
        selected.push( { text: $this.text(), value: $this.val() } );
    });
    return selected;
}

回答by Mike Trpcic

If all of your select boxes start with a similar id ("select_1", "select_2", "select_3", etc), you can just do:

如果您所有的选择框都以相似的 id 开头(“select_1”、“select_2”、“select_3”等),您可以这样做:

var arr = [];
$("select[id^='select_']").children('option:selected').each(function(){
  //you will do this once for every selected item...
}

This allows you to loop through only specific select boxes, in case you have multiple groupings.

这允许您仅循环遍历特定的选择框,以防您有多个分组。

回答by redsquare

//Another option

var selected = [];

$('select :has(:selected)').each( function(){

    var $this = $(this);
    selected.push( { text: $this.text(), value: $this.val() );

});

return selected;

回答by Ken

https://jsfiddle.net/kmgoddard/Lfkvm3ar/4/

https://jsfiddle.net/kmgoddard/Lfkvm3ar/4/

$("#mybutton").click(function(){
list = new Array();
$('select > option:selected').each(function() {

  list.push($(this).val());

});
 alert(list.toString());
});