<select>
<option value="test">label </option>
</select>
該值可以通過$select.val()
檢索。如何使用jQuery獲取選擇選項的標籤?
那麼label
呢?
是否有解決方案,將在IE6中工作?
<select>
<option value="test">label </option>
</select>
該值可以通過$select.val()
檢索。如何使用jQuery獲取選擇選項的標籤?
那麼label
呢?
是否有解決方案,將在IE6中工作?
試試這個:
$('select option:selected').text();
這並不總是正確的。該選項的顯示描述也可以通過'label'屬性來指定(除了<= IE7)。請參閱http://www.w3schools.com/tags/att_option_label.asp#gsc.tab=0和http://www.w3.org/TR/html401/interact/forms.html#h-17.6 – 2013-04-02 18:47:46
獲取_label_屬性,可以使用:jQuery('#theid option:selected')。attr('label') – 2014-04-10 16:07:59
<SELECT id="sel" onmouseover="alert(this.options[1].text);"
<option value=1>my love</option>
<option value=2>for u</option>
</SELECT>
$("select#selectbox option:eq(0)").text()
的0指數中的 「選項:EQ(0)」 即可交換您想要檢索的索引選項。
這是有幫助的: http://www.myphpetc.com/2009/03/jquery-select-element-cheat-sheet.html
嗨先給一個ID,選擇作爲
<select id=theid>
<option value="test">label </option>
</select>
,那麼你可以撥打所選的標籤那樣:
jQuery('#theid option:selected').text()
這正是我一直在尋找的! – DirtyBirdNJ 2013-08-12 05:10:44
要獲得一個特定的選項在下拉列表中的標籤喲可以這樣 -
$('.class_of_dropdown > option[value='value_to_be_searched']').html();
或
$('#id_of_dropdown > option[value='value_to_be_Searched']').html();
爲了參考還存在的選項標籤的二次label
屬性:
//returns "GET THIS" when option is selected
$('#selecter :selected').attr('label');
的Html
<select id="selecter">
<option value="test" label="GET THIS">
Option (also called label)</option>
</select>
嘗試這種情況:
$('select option:selected').prop('label');
這將拉出顯示文本兩種風格<option>
元素:
<option label="foo"><option>
- >"foo"
<option>bar<option>
- >"bar"
如果它既有一個label
屬性和文本在元素內部,它將使用label
屬性,這與瀏覽器的行爲相同。
留給後人,這是jQuery的3.1.1
下測試的創工作Plunker這一點。 https://plnkr.co/edit/vR9aGoCwoOUL9tevIEen $('#console').append("<br/>"+$('#test_s :selected').text())
您的意思是如何獲得選定的值,選定的值?這是你的情況標籤? – ant 2010-02-01 09:47:47
這個問題應該重寫爲「如何獲得jQuery的選擇選項**文本**」?並且所有對標籤的引用都應該用文本替換以避免與標籤屬性混淆。 – 2015-11-13 00:35:55