问题
I have a select menu that i'm trying to bind a click function and be able to get the select options value attribute back on change.
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
});
回答1:
See this:
$("#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);
});
回答2:
this.value
generally you can use it with form elements and it is much faster then wrapping this as a jQuery object and then extracting the value.
However in some cases, specifically with select's, you can run into issues in older versions of IE when options with no explicity value="", and jquery takes care of this for you with...
$(this).val();
回答3:
select.bind( "change", function(event, ui) {
$(this).children(':selected').val();
});
This will select the selected <option>(s).
Here is a demo: http://jsfiddle.net/4KS9z/1/
来源:https://stackoverflow.com/questions/8484940/how-do-i-get-the-value-of-a-select-menu-with-jquery-on-change