Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile Change DropDown Selected Option and refresh it

I am writing jQuery Mobile App. I am changing drop-down selected option via below statement:- $("#DataBaseNames").val(db);

I am sure about correct db value being passed, as i checked it via alert. When I drill down the drop down, it also shows the correct text selected, but dropdown itself is not showing the correct text as selected.

Any refresh call I need to insert?

Edit:-Adding code, below answer from phill solved it

<script type="text/javascript">   

        $("#@ViewBag.DivTitle").live('pageshow', function () {

            var db = getCookie("DataBaseNames");

            $("#DataBaseNames").val(db);            
            $("#DataBaseNames option[value='"+ db + "']").attr("selected", "selected");

            //      refresh value , Following is what is required        
            $('select').selectmenu('refresh');

            $("#cmdLogOn").live("click", function () {
                var dbSelected = $("#DataBaseNames option:selected").text();              
                setCookie('DataBaseNames', dbSelected);
            });
        });

        function setCookie(name, value) {
          var expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function getCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }
    </script>
like image 369
Anand Avatar asked Nov 16 '11 14:11

Anand


2 Answers

refresh update the custom select

This is used to update the custom select to reflect the native select element's value.If the number of options in the select are different than the number of items in the custom menu, it'll rebuild the custom menu. Also, if you pass a true argument you can force the rebuild to happen.

//refresh value         
$('select').selectmenu('refresh');

//refresh and force rebuild
$('select').selectmenu('refresh', true);

Docs:

  • http://jquerymobile.com/demos/1.0rc3/docs/forms/selects/methods.html
like image 52
Phill Pafford Avatar answered Sep 18 '22 05:09

Phill Pafford


I checked this link and found it working for me. Try this:

var myselect = $("select#YourDropdownID");
myselect[0].selectedIndex =0;
myselect.selectmenu("refresh");
like image 31
Silver Avatar answered Sep 22 '22 05:09

Silver