How to get all options of a select using jQuery?
How can I get all the options of a select through jQuery by passing on its ID?
I am only looking to get their values, not the text.
Use:
$("#id option").each(function()
{
// Add $(this).val() to your list
});
.each() | jQuery API Documentation
我不知道jQuery,但我知道如果你得到select元素,它包含一个'options'对象。
var myOpts = document.getElementById('yourselect').options;
alert(myOpts[0].value) //=> Value of the first option
$.map
is probably the most efficient way to do this.
var options = $('#selectBox option');
var values = $.map(options ,function(option) {
return option.value;
});
You can add change options to $('#selectBox option:selected')
if you only want the ones that are selected.
The first line selects all of the checkboxes and puts their jQuery element into a variable. We then use the .map
function of jQuery to apply a function to each of the elements of that variable; all we are doing is returning the value of each element as that is all we care about. Because we are returning them inside of the map function it actually builds an array of the values just as requested.
上一篇: 使用jQuery获取所选选项的文本
下一篇: 如何使用jQuery获取选择的所有选项?