2011-12-13 21 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/

相关问题