jquery selected dropdown返回true或false

时间:2014-09-24 13:14:15

标签: jquery select drop-down-menu selected

我有一个下拉列表,我想使用jquery来查看所选的选项是否包含其中的选定字段<option id="id" class="class" selected >Option</option> jquery中是否有一个方法可以返回true或false,具体取决于它是什么

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery检查所选项目的所有内容。

// get the selected item 

// note: this is getting the parent 'select' element first and then finding 
//       the selected option within the selects child elements

var selectedOption = $('select').find('option:selected');

// you could also use a selector to find a specific select within the page, e.g.,
var select = $('select.my-select'); // this will find a select with a class attribute of 'my-select'

// get the text from it
var text = selectedOption.text();

// get the html from it
var html = selectedOption.html();

// get an attribute from it
var classNames = selectedOption.attr('class');
var id = selectedOption.attr('id');