jQuery Mobile Change DropDown Selected Option并刷新它 [英] jQuery Mobile Change DropDown Selected Option and refresh it

查看:105
本文介绍了jQuery Mobile Change DropDown Selected Option并刷新它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写jQuery Mobile App. 我通过以下语句更改下拉选择的选项:- $(#DataBaseNames").val(db);

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

我肯定会传递正确的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?

-添加代码,下面phill的答案已解决

-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>

推荐答案

刷新更新自定义选择

这用于更新自定义选择以反映本机选择元素的值. 选择中的选项与项目中的项目数不同 自定义菜单,它将重建自定义菜单.另外,如果您通过了 您可以强制进行重建.

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);

文档:

这篇关于jQuery Mobile Change DropDown Selected Option并刷新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆