2011-12-13 59 views
0

我有一個選擇菜單,我試圖綁定一個點擊功能,並能夠獲得選擇選項值屬性的變化。我如何獲得jQuery的選擇菜單的值更改

   select = $('#select_networks').selectmenu(); 

      //bind the change of network/group 
      select.bind("change", function(event, ui) { 

       //need to figure out the selected elements value attribute 

      }); 

回答

2

看到這個:

$("#select_networks").bind("change", function() { 
    alert($(this).val()); 
}); 

UPDATE

$("#select_networks").bind("change", function() { 
    var typeVal = $(this).children("*[value=" + $(this).val() + "]").attr("type"); 
    alert(typeVal); 
}); 
+0

這引起了值正常,但跟進的問題。我怎樣才能拉任意屬性。例如,我在select選項中設置了一個類型字段,我嘗試了alert($(this).attr(「type」));但我只是不確定。 – Brian

+0

更新了代碼。 –

1
this.value 

通常可以用表單元素使用它,它是包裝然後作爲this jQuery對象,然後提取所述值快得多。

在某些情況下

然而,特別是與選擇的,你可以在舊版本的IE遇到問題時,沒有明確地value=""和jQuery選擇需要照顧這對你...

$(this).val(); 
1
 select.bind("change", function(event, ui) { 

      $(this).children(':selected').val(); 

     });  

這將選擇所選的<option>(s)。

這裏是一個演示:http://jsfiddle.net/4KS9z/1/