JQuery Mobile - Getting SelectMenu Value

When I try to get a value from my selection menu, I get an empty value.

I know that the reason is that jQuery mobile markup results in two elements with the class "mySelect" - so the $ (". SelectMenu") selector is a wrapped set.

I am wondering if there is a way to get the value through a plugin like $ (". MySelect"). selectmenu ("value") or something like that.

<section data-role="page"> <section data-role="content"> <input type="button" value="what is the value" id="myButton"/> <select class="mySelect"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select> </section> </section> $("#myButton").click(function(){ var val = $(".mySelect").val(); alert(val); }); 

http://jsfiddle.net/9HG9t/

+4
source share
3 answers

You can do this to get the value.

 $(".mySelect option:selected").val() 

Example: http://jsfiddle.net/9HG9t/1/

+9
source

Just use the classic jQuery way:

 $("#myButton").click(function(){ var val = $(".mySelect").find(":selected").text(); alert(val); }); 

Real-time example: http://jsfiddle.net/Gajotres/ANVQb/

+3
source

Your code works in a Visual Studio project enclosed in a <script> block.

0
source

All Articles