2010-11-22 26 views
4
<select id="selTest"> 
    <option custom="123" value="abc">111</option> 
    <option custom="456" value="def">222</option> 
<select> 

...使用jQuery,如何獲取option.custom其中option.value = selTest.val()得到一個自定義標籤值使用jQuery

換句話說

:如果selTest.val =閃避,那麼$ X = 456(我試圖讓定製到$ X的值)

+0

我就把你贊成的東西非驗證自定義屬性,像`。數據()` – Stephen 2010-11-22 17:32:35

回答

6

試試這個:

var selectedCustomValue = $("select#selTest option:selected").attr("custom"); 
0
var selectElement = $("#selTest")[0]; 
alert(selectElement.options[selectElement.selectedIndex].getAttribute("custom")); 
+0

爲什麼不走一路和使用'的document.getElementById(「selText」)`而不是`$('#selTest')[0]`當你不在第二行使用jQuery? :) – 2010-11-22 17:21:56

0

嘗試以下

$('#selTest').change(function() { 
    var value = $(this).val(); 
    var custom = $('option[value="' + value +'"]').attr('custom'); 
    ... 
}); 
相關問題