简体   繁体   English

jQuery Mobile弹出窗口没有打开.popup('open')

[英]jQuery Mobile popup is not opening on .popup('open')

I am trying to use jQuery Mobile 1.3.1's popup to warn the user when login credentials are false. 我试图使用jQuery Mobile 1.3.1的弹出窗口在登录凭据为假时警告用户。 I started with a basic template from jquerymobile's documentation, but I couldn't make it work with $('#popupBasic').popup('open'); 我从jquerymobile的文档开始使用基本模板,但我无法使用$('#popupBasic').popup('open'); If I use it this way; 如果我这样使用它;

     <div data-role="page">


        <div data-role="header" data-tap-toggle="false">
        </div><!-- /header -->

        <div data-role="content">

            <a href="#popupBasic" data-rel="popup">Tooltip</a>
            <div data-role="popup" id="popupBasic">I will change this text dynamically if this popup works</div>


        </div><!-- /content -->
    </div><!-- /page -->

It works well when I click on the Tooltip link. 单击Tooltip链接时效果很好。 But in my case there isn't any click so I am trying this; 但在我的情况下,没有任何点击,所以我正在尝试这个;

                if(retVal){
                    $.mobile.changePage('index');
                }
                else{                    
                    $('#popupBasic').popup();
                    $('#popupBasic').popup("open");
                }

this is after my ajax login function makes a callback, so retVal is true if there isn't any errors, false if there is (and at that point I am trying to show popup). 这是在我的ajax登录函数进行回调之后,所以如果没有任何错误,则retVal为true,如果存在则为false(并且此时我试图显示弹出窗口)。 By the way all my js part is in 顺便说一下,我所有的js部分都在

 $(document).on('pageinit', function(){});

so i wait till jquerymobile is ready for the page. 所以我等到jquerymobile为页面做好准备。

What happens when I do this is on desktop browsers link changes as 当我这样做时,会在桌面浏览器链接更改上发生什么

http://localhost/login#&ui-state=dialog

but doesn't show the pop up. 但没有显示弹出窗口。 After some refreshes and caches it starts to show. 经过一些刷新和缓存后,它开始显示。 On iOS same thing also happens but on android sometimes it changes link some time it doesn't. 在iOS上同样的事情也发生了但是在Android上有时它会改变链接,有时它不会。

I would be really happy if someone can help me to solve this problem. 如果有人能帮我解决这个问题,我会很高兴。 Thanks in advance. 提前致谢。

That's because when pageinit is fired, the poupup isnt ready for manipulation just yet. 那是因为当pageinit被触发时,poupup还没有准备好进行操作。 You need to use pageshow to get the popup to open. 您需要使用pageshow来打开弹出窗口。 Here's what you do : 这是你做的:

  • Make the ajax call in pageinit . pageinit进行ajax调用。 store the data in data attribute of the page. 将数据存储在页面的data属性中。
  • Then, in the pageshow event, take if from data and manipulate it the way you want, then open the popup. 然后,在pageshow事件中,从数据中获取if并按照您想要的方式操作它,然后打开弹出窗口。

Here's the code : 这是代码:

$(document).on({
    "pageinit": function () {
        alert("pageinit");
        //$("#popupBasic").popup('open'); will throw error here because the page is not ready yet
        //simulate ajax call here
        //data recieved from ajax - might be an array, anything
        var a = Math.random();
        //use this to transfer data betwene events
        $(this).data("fromAjax", a);
    },
    //open popup here
    "pageshow": function () {
        alert("pageshow");
        //using stored data in popup
        $("#popupBasic p").html("Random : " + $(this).data("fromAjax"));
        //open popup
        $("#popupBasic").popup('open');
    }
}, "#page1");

And here's a demo : http://jsfiddle.net/hungerpain/MvwBU/ 这是一个演示: http//jsfiddle.net/hungerpain/MvwBU/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM