jquery popup close function not working

柔情痞子 提交于 2019-12-11 06:57:06

问题


I have button on my mobile website. I want to do following:

when I press the button the popup should appear. This popup should contain some text and a OK button. When I press Ok button the popup should disapear, nothing else. The page should stay in the same conditions.

My code is as follows:

<a id="edit-btn2" data-rel="popup" data-transition="slide" data-position-to="window" class= "edit-button1"  data-role="button" href="#popupPanel"></a>

<div data-role="popup" id="popupPanel" data-dismissible='false' data-corners="false" data-theme="b"  style="height:100px; width:300px;">
  <p>This is just a demonstrator</p>
  <button href="#" id= "popup-button"  data-theme="c" data-icon="false" data-mini="false"  >OK</button>
</div>

The javascript is as follows:

$( "#popup-button" ).click(function() {
  $( "#popupPanel" ).popup( "close" );
});

Current situation is:

When I click on the ok button it works for the very first time but not latter.


回答1:


Use <a> instead of <button>. Your code should look like this:

<a id="edit-btn2" data-rel="popup" data-transition="slide" data-position-to="window" class="edit-button1" data-role="button" href="#popupPanel"></a>

<div data-role="popup" id="popupPanel" data-dismissible="false" data-corners="false" data-theme="b" style="height:100px; width:300px;">
  <p>This is just a demonstrator</p>
  <a href="#" data-role="button" data-rel="back" id="popup-button" data-theme="c" data-icon="false" data-mini="false" >OK</a>
</div>

Demo

I've also added

data-role="button" data-rel="back"

Javascript isn't necessary in this case




回答2:


Wrap your code in pageinit and use correct event binding using .on.

$(document).on("pageinit", function () {
    $("#popup-button").on("click", function () {
        $("#popupPanel").popup("close");
    });
});

Or, add data-rel="back" attribute to button. However, if you wish to use this, it's safer to add data-history="false" in order not to go back to previous page.

<button href="#" id= "popup-button"  data-theme="c" data-rel="back" data-history="false">OK</button>

Demo




回答3:


Use Class instead of id for button.

Html - button

href="#" class= "popup-button"  data-theme="c" data-icon="false" data-mini="false"

JavaScript

$( ".popup-button" ).click(function() {
  $( "#popupPanel" ).popup( "close" );
});


来源:https://stackoverflow.com/questions/20213330/jquery-popup-close-function-not-working

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