最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JQuery Mobile: inline data-role="page" javascript being retain when page is released from DOM? - Stack Overflo

programmeradmin1浏览0评论

Using this call <a href="deleteDialog.html" data-rel="dialog" data-transition="pop" data-role="button" id='deleteDialog'>Delete</a> to get the following dialog page:

<div data-role="page" id="deleteCompanyDialog">
<script type="text/javascript">

$("#deleteButton").live("click", function() {
        alert("this alert increments");

});

</script>

    <div data-role="header" data-theme="d">
        <h1>Dialog</h1>

    </div>

    <div data-role="content" data-theme="c">
        <h1>Delete Company</h1>
        <p id="message"></p>
        <a data-role="button" data-theme="b" id="deleteButton" >Sounds good</a>       
        <a href="pany.jsp" data-role="button" data-rel="back" data-theme="c">Cancel</a>    
    </div>
</div>

seems to retain the live("click".. binding from any previous calls to this dialog and then binds the live call again. So if I call the page 4 separate times, on the forth dialog page call it will popup 4 alert screens. Is there a way to have the javascript still be within data-role="page" so it can load with ajax but not increment the "live" binding. I tried $("#deleteCompanyDialog").live("pagecreate"... as well as pageload (a long shot) which does not work either.

Help would be greatly appreciated.

Using this call <a href="deleteDialog.html" data-rel="dialog" data-transition="pop" data-role="button" id='deleteDialog'>Delete</a> to get the following dialog page:

<div data-role="page" id="deleteCompanyDialog">
<script type="text/javascript">

$("#deleteButton").live("click", function() {
        alert("this alert increments");

});

</script>

    <div data-role="header" data-theme="d">
        <h1>Dialog</h1>

    </div>

    <div data-role="content" data-theme="c">
        <h1>Delete Company</h1>
        <p id="message"></p>
        <a data-role="button" data-theme="b" id="deleteButton" >Sounds good</a>       
        <a href="pany.jsp" data-role="button" data-rel="back" data-theme="c">Cancel</a>    
    </div>
</div>

seems to retain the live("click".. binding from any previous calls to this dialog and then binds the live call again. So if I call the page 4 separate times, on the forth dialog page call it will popup 4 alert screens. Is there a way to have the javascript still be within data-role="page" so it can load with ajax but not increment the "live" binding. I tried $("#deleteCompanyDialog").live("pagecreate"... as well as pageload (a long shot) which does not work either.

Help would be greatly appreciated.

Share Improve this question asked Jan 19, 2012 at 17:37 GregGreg 4421 gold badge5 silver badges18 bronze badges 1
  • data-role="Dialog" is at equal position as data-role="page", so if you try to put it inside a page, it won't work. – bobek Commented Jan 19, 2012 at 17:40
Add a ment  | 

1 Answer 1

Reset to default 7

Instead of using .live(), use .bind() and place your JavaScript in a delegated event handler:

<div data-role="page" id="deleteCompanyDialog">

    <div data-role="header" data-theme="d">
        <h1>Dialog</h1>

    </div>

    <div data-role="content" data-theme="c">
        <h1>Delete Company</h1>
        <p id="message"></p>
        <a data-role="button" data-theme="b" id="deleteButton" >Sounds good</a>       
        <a href="pany.jsp" data-role="button" data-rel="back" data-theme="c">Cancel</a>    
    </div>
<script type="text/javascript">

$(document).delegate("#deleteCompanyDialog", "pageinit", function() {
    $("#deleteButton").bind('click', function () {
        alert("this alert DOES NOT increment");
    });
});

</script>
</div>

This is like using $(function () {}); but for jQuery Mobile. The pageinit event will fire when the page is initialized (happens once per pseudo-page) and the .bind() function call will only bind to elements present in the DOM. When you use .live() it doesn't bind to the actual element, it binds to the document element, which does not get removed when you navigate away from the dialog (so each time you show the dialog you add another delegated event handler).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论