jQuery获取所有下拉列表值 [英] jquery getting all dropdown values

查看:226
本文介绍了jQuery获取所有下拉列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ID为my_dropdown的下拉菜单<select>.我允许多选.我知道如何使用jquery提取选定的值:

I have dropdown <select> with id my_dropdown. I allow multi-select. I know how to pull the selected value(s) using jquery:

var values = $('#my_dropdown').val();

如果我选择多个,这将返回一个值数组.无论选择什么,我还需要获取下拉列表中的所有值.如何使用jquery类似地获取给定id的下拉列表中的所有值?

This will return an array of values if I select multiple. I also need to get all the values in the dropdown regardless of what is selected. How can I use jquery similarly to get all the values in the dropdown given that id?

推荐答案

诸如此类的事情

var values = [];
$('#my_dropdown option').each(function() { 
    values.push( $(this).attr('value') );
});

这篇关于jQuery获取所有下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆