How do I get the value of a select menu with jquery on change

*爱你&永不变心* 提交于 2019-12-31 06:22:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!