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

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

问题描述

我正在编写 jQuery 移动应用程序.我正在通过以下语句更改下拉选择的选项:-$("#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 更改 DropDown Selected Option 并刷新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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