2014-09-24 33 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');