2014-09-24 46 views
0

我有一个下拉列表,我想用jquery查看选中的选项是否有选中的字段<option id="id" class="class" selected >Option</option> jquery中是否有一个方法可以做到这一点,这将返回true或false取决于它是什么jquery选中下拉列表返回true或false

+2

,因为选项有一个id,你可以'$(“#ID”)是(“:选择”)。' – 2014-09-24 13:17:04

+0

展开你的问题,请... – Hackerman 2014-09-24 13:18:01

+0

阿伦P约翰尼拉这正是我想要的可能你可以写下这个答案 – user3956534 2014-09-24 13:40:22

回答

0

ou可以使用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');