我如何通过jQuery传递它的ID来获得一个选择的所有选项?

我只是想获取它们的值,而不是文本。


当前回答

对于多选选项:

$('#test').val()返回所选值列表。 $(" #测试选项”)。Length返回选项总数(包括选中的和未选中的)

其他回答

对于多选选项:

$('#test').val()返回所选值列表。 $(" #测试选项”)。Length返回选项总数(包括选中的和未选中的)

$('select#id').find('option').each(function() {
    alert($(this).val());
});

这将把#myselectbox的选项值放入一个整洁的数组中:

// First, get the elements into a list
var options = $('#myselectbox option');

// Next, translate that into an array of just the values
var values = $.map(options, e => $(e).val())
$("select#MY_SELECT_ID").find('option').each(function() {
    console.log($(this).val());
    console.log($(this).text());
});

Use:

$("#id option").each(function()
{
    // Add $(this).val() to your list
});

.each() | jQuery API文档