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