Prevent JQuery Mobile from closing a popup when user taps outside of it

佐手、 提交于 2019-12-04 23:03:33

This has been added as a feature request on Github. See issue here.

As a hack for this in the interim is to unbind the events on the ui-popup-screen. I would put the following code in the pageinit.

$("#yourPopupId").on({
    popupbeforeposition: function () {
        $('.ui-popup-screen').off();
    }
});

This is a heavy handed quickfix, but it works.

For future searchers, as of 1.3, this is now supported. Simply add the data-dismissible="false" attribute to the panel div.

Building on @TheGwa's excellent solution, here's a generalization to prepare for the upcoming official feature (presumably, in version 1.3):

  • Add data-dismissible='false' to the markup of all popups that you don't want to be dismissible by tapping outside of them.

  • Add the following event handler to your app; it will handle all popups:

_

$(window).on('popupbeforeposition', 'div:jqmData(role="popup")', function() {
        var notDismissible = $(this).jqmData('dismissible') === false;
        if (notDismissible) {
          $('.ui-popup-screen').off();
        }
});

-

Once the feature is officially supported, simply remove the event handler.

Note that, sadly, the official documentation (as of 1.2) suggests that the feature is already available, but it is not - see http://jquerymobile.com/test/docs/pages/popup/options.html

Rupali

Add data-dismissible="false" in following way.

<div data-role="popup" id="popupnotification" data-overlay-theme="b" data-theme="c"  data-position-to="window" style="max-width:600px;">

@ MJB -> If you want to be able to click anywhere (e.g. on a button) when a pop-up is active, you can alter the CSS of the pop-up as follows:

.ui-popup-screen{
                    position: relative;
                }

This worked for me.

Note: Pop-up does not close on click anymore when you do this - I made a workaround:

$(window).click(function() {    
    if ($( "#myPopup-popup" ).hasClass("ui-popup-active")){
        $( "#myPopup" ).popup( "close" );
    }
});

the myPopup-popup ID is generated by JQM - myPopup is the ID I gave the pop-up.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!