jQuery 遍历 <select> 选项

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

Iterate through <select> options

jquery

提问by user208662

I have a <select>element in HTML. This element represents a drop down list. I'm trying to understand how to iterate through the options in the <select>element via JQuery.

<select>在 HTML 中有一个元素。此元素表示一个下拉列表。我试图了解如何<select>通过 JQuery遍历元素中的选项。

How do I use JQuery to display the value and text of each option in a <select>element? I just want to display them in an alert()box.

如何使用 JQuery 显示<select>元素中每个选项的值和文本?我只想把它们显示在一个alert()盒子里。

回答by karim79

$("#selectId > option").each(function() {
    alert(this.text + ' ' + this.value);
});

回答by IT ppl

This worked for me

这对我有用

$(function() {
    $("#select option").each(function(i){
        alert($(this).text() + " : " + $(this).val());
    });
});

回答by Arun Pratap Singh

can also Use parameterized each with index and the element.

也可以使用每个带索引和元素的参数化。

$('#selectIntegrationConf').find('option').each(function(index,element){
 console.log(index);
 console.log(element.value);
 console.log(element.text);
 });

// this will also work

// 这也能工作

$('#selectIntegrationConf option').each(function(index,element){
 console.log(index);
 console.log(element.value);
 console.log(element.text);
 });

回答by rogerdpack

And the requisite, non-jquery way, for followers, since google seems to send everyone here:

对于追随者来说,必不可少的非 jquery 方式,因为谷歌似乎将每个人都发送到这里:

  var select = document.getElementById("select_id");
  for (var i = 0; i < select.length; i++){
    var option = select.options[i];
    // now have option.text, option.value
  }

回答by Dulith De Costa

You can try like this too.

你也可以这样试试。

Your HTMLCode

你的HTML代码

<select id="mySelectionBox">
    <option value="hello">Foo</option>
    <option value="hello1">Foo1</option>
    <option value="hello2">Foo2</option>
    <option value="hello3">Foo3</option>
</select>

You JQueryCode

JQuery编码

$("#mySelectionBox option").each(function() {
    alert(this.text + ' ' + this.value);
});

OR

或者

var select =  $('#mySelectionBox')[0];

  for (var i = 0; i < select.length; i++){
    var option = select.options[i];
    alert (option.text + ' ' + option.value);
  }

回答by Yojana Ambavkar

$.each($("#MySelect option"), function(){
                    alert($(this).text() + " - " + $(this).val());                    
                });

回答by zzart

Another variation on the already proposed answers without jQuery.

没有 jQuery 的已经提出的答案的另一种变体。

Object.values(document.getElementById('mySelect').options).forEach(option => alert(option))

回答by Jeyko Caicedo

If you don't want Jquery (and can use ES6)

如果你不想要 Jquery(并且可以使用 ES6)

for (const option of document.getElementById('mySelect')) {
  console.log(option);
}