Javascript 框。这是最优雅的解决方案。Map是一个非常强大的结构,完全适合这种情况。我真的很喜欢你的解决方案-这是我采用的方法。但是,如果您只需要选定的值,则更为简单:$(“#selectBox”).val()将返回选定值的数组。您好,@PCasagrande我有一个

Javascript 框。这是最优雅的解决方案。Map是一个非常强大的结构,完全适合这种情况。我真的很喜欢你的解决方案-这是我采用的方法。但是,如果您只需要选定的值,则更为简单:$(“#selectBox”).val()将返回选定值的数组。您好,@PCasagrande我有一个,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,框。这是最优雅的解决方案。Map是一个非常强大的结构,完全适合这种情况。我真的很喜欢你的解决方案-这是我采用的方法。但是,如果您只需要选定的值,则更为简单:$(“#selectBox”).val()将返回选定值的数组。您好,@PCasagrande我有一个自定义属性名称“数据类型”。使用您的解决方案,我如何获得此解决方案的价值?我正在做的是“option.data type”,但这给了我一个NaN。提前谢谢。您好,@PCasagrande我通过执行“$(选项,this).attr(“数据类型”)


框。这是最优雅的解决方案。Map是一个非常强大的结构,完全适合这种情况。我真的很喜欢你的解决方案-这是我采用的方法。但是,如果您只需要选定的值,则更为简单:
$(“#selectBox”).val()
将返回选定值的数组。您好,@PCasagrande我有一个自定义属性名称“数据类型”。使用您的解决方案,我如何获得此解决方案的价值?我正在做的是“option.data type”,但这给了我一个NaN。提前谢谢。您好,@PCasagrande我通过执行“$(选项,this).attr(“数据类型”)得到了我所需要的。但如果你有更好的,一定要告诉我。谢谢。:)我认为您可以只做“returnoption.attr(“数据类型”);”在地图上。这将为您提供一个数据类型值数组。JavaScript操作都不需要jQuery。jQuery是简化JS操作的一个选择。您也可以这样做:$(“#id option”).each(函数(name,val){var opt=val.text;})
$.map
更优雅,更不容易出错(见下文)。
$(“#id option”).map((index,option)=>option.value)
:注意回调签名是如何与“香草”JS
map
函数(
(项,索引)=>
)相反的。attr('selected',selected')?这是票据,因为我喜欢将元素缓存到init上的变量中,而不是使用选择器,
var myOpts = document.getElementById('yourselect').options;
alert(myOpts[0].value) //=> Value of the first option
$("#id option").each(function()
{
    // Add $(this).val() to your list
});
$('select#id').find('option').each(function() {
    alert($(this).val());
});
// 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#example option").map(function() {return $(this).val();}).get();
var selected_val = $.map($("input[name='d_name']:checked"), function(a)
    {
        return a.value;
    }).join(',');

alert(selected_val);
var options = $('#selectBox option');

var values = $.map(options ,function(option) {
    return option.value;
});
$("#id option").each(function()
{
    $(this).prop('selected', true);
});
$("#id option").each(function(){
    $(this).attr('selected', true);
});
$("input[type=checkbox][checked]").serializeArray();
$(".some_class[type=checkbox][checked]").serializeArray();
alert($("input[type=checkbox][checked]").serializeArray().toSource());
var values = $.map($('#selectBox option'), function(ele) {
   return ele.value; 
});
var assignedRoleId = new Array();
$('#RolesListAssigned option').each(function(){
    assignedRoleId.push(this.value);
});
$('#test').find("select option:contains('B')").filter(":selected");
$('#nCS1 > option').each((index, obj) => {
   console.log($(obj).val());
})
$(() => {
$('#myselect option').each((index, data) => {
    console.log(data.attributes.value.value)
})})
export function GetSelectValues(id) {
const mData = document.getElementById(id);
let arry = [];
for (let index = 0; index < mData.children.length; index++) {
    arry.push(mData.children[index].value);
}
return arry;}
    var arr = [], option='';
$('select#idunit').find('option').each(function(index) {
arr.push ([$(this).val(),$(this).text()]);
//option = '<option '+ ((result[0].idunit==arr[index][0])?'selected':'') +'  value="'+arr[index][0]+'">'+arr[index][1]+'</option>';
            });
console.log(arr);
//$('select#idunit').empty();
//$('select#idunit').html(option);