2010-09-21 64 views
0

這是一個確定段落/選項框的選定值的連接方式,其ID爲「shirt_size_1221」?jquery價值替代

var user_id = '1221'; 
x = "#shirt_size_" + user_id ;       
alert($(x + " option:selected")); 

回答

0

alert聲明將始終顯示[object],除非你將其更改爲

alert($(x+' option:selected').val()); 

不要擔心,這是獲得所選選項的一個好辦法。

0

您可以通過使用

var selectList = document.getElementById("shirt_size_1221"); 
var val = selectList.options[selectList.selectedIndex].text; 

沒有JQuery的需要得到在下拉框中選擇的選項的值。

0

「正確」 的方式是簡單:

var user_id = '1221'; 
x = "#shirt_size_" + user_id ;   
alert($(x).val()); 
+0

文檔[這裏](http://api.jquery.com/val/) – sje397 2010-09-21 02:22:08