Answers:
$("#selectId > option").each(function() {
alert(this.text + ' ' + this.value);
});
也可以使用带有索引和元素的参数化的每个参数。
$('#selectIntegrationConf').find('option').each(function(index,element){
console.log(index);
console.log(element.value);
console.log(element.text);
});
//这也将起作用
$('#selectIntegrationConf option').each(function(index,element){
console.log(index);
console.log(element.value);
console.log(element.text);
});
您也可以尝试这样。
您的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>
您JQuery
编码
$("#mySelectionBox option").each(function() {
alert(this.text + ' ' + this.value);
});
要么
var select = $('#mySelectionBox')[0];
for (var i = 0; i < select.length; i++){
var option = select.options[i];
alert (option.text + ' ' + option.value);
}
$.each($("#MySelect option"), function(){
alert($(this).text() + " - " + $(this).val());
});
如果您不需要Jquery(并且可以使用ES6)
for (const option of document.getElementById('mySelect')) {
console.log(option);
}