2011-03-29 143 views

回答

7
$('#mySelect').val(); 

會給與所選擇的選項相關聯的值(如果存在的value屬性,或文本以其他方式)。


$('#mySelect')[0].selectedIndex; 

會給所選擇的選項的索引。


$('#mySelect option:selected'); 

會給所選的選項元素,從中你可以抓住:

$('#mySelect option:selected').text(); // the text 
$('#mySelect option:selected').val(); // the value 
0

調用.val()功能與表單元素上沒有的參數將返回該元素的當前值。在select元素(下拉框)的情況下,它將是當前選擇的option元素的值。

1
<select id="selectid"> 
<option value="1">One</option> 
<option value="2">Two</option> 
<option value="3">Three</option> 
</select> 

假設用戶選擇第二個選項。

獲取選中的選項

$("select#selectid").val(); // Returns 2 

的價值獲取選中的選項的文本

$("#selectid option:selected").text(); // Returns Two 

的jsfiddle鏈接:http://jsfiddle.net/gpmattoo/LBRCx/

相關問題