【问题标题】:jQuery Mobile: How to make dialog open more than oncejQuery Mobile:如何使对话框多次打开
【发布时间】:2016-08-22 18:07:11
【问题描述】:

我不确定这是否是一个错误,或者它是否是 jQuery Mobile 中对话框的标准方法,但我有一个 dialog 框,当您单击按钮并新加载页面时会打开该框。但是,当您关闭它并再次打开它时,它会闪烁打开并关闭。有没有办法让对话框多次打开??

编辑:我已经编辑了我的代码以包含第二个data-role="page",因为我的问题只出现在第二页上。

这是我的代码:

HTML:

 <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>List App</title>

    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <script src="../JS/jquery.mobile.icons-1.4.5.min.css"></script>
    <script src="../JS/list.js"></script>

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
    <link rel="stylesheet" href="../CSS/stylesheet.css"/>
    <link rel="stylesheet" href="../CSS/jquery.mobile.icons-1.4.5.min.css"/>
    <link rel="stylesheet" href="../CSS/jquery.mobile.inline-png-1.4.5.min.css"/>

    </head>

     <body>

<!--FIRST PAGE-->   
<div data-role="page" id="filterPage">

        <div data-role="header" data-theme="b">
             <h1>List App</h1>
       </div>

        <div data-role="content">
            <ul data-role="listview" id="chipperList" data-filter="true" data-filter-reveal="false" data-filter-placeholder="Chose a County..." data-divider-theme="a" data-inset="false" data-transition="slide" >
                <li data-role="list-divider" data-theme="b" role="heading" >County</li>
            </ul>
        </div>
 </div>  

<!--SECOND PAGE-->
        <div data-role="page" id="listDetails">
            <div data-role="header" data-theme="b">
             <h1>List App</h1>
             <div data-role="navbar" data-rel="back">
              <a href="#filterPage" class="ui-btn ui-icon-search ui-btn-icon-left">Return to Search</a>
              </div>
            </div>

    <div data-role="content"></div>



        <div data-role="footer" data-theme="b" data-position="fixed">
            <div data-role ="navbar">    
                <ul>
        <li><a href="#freeCoupon" data-rel="dialog" data-transition="pop">Free Coupon</a></li> 


                </ul>
            </div>
            </div>


    </div> 


<!--COUPON--> 
         <div data-role="dialog" id="freeCoupon"> 
             <div data-role="header" data-theme="b"> 
             <h1>Congratulations!</h1>      
    </div>
             <div data-role ="content">
                <img src="../CSS/IMAGES/coupon.jpg"/>
             </div> 
         </div>

    </body>   

    </html>

LIST.JS

$(document).on("pageinit", "#filterPage", function () {
$.getJSON("../JSON/list_data.json", function(info){

    var li = "";
    $.each(info, function (i, name) {
        li += '<li><a href="#" id="' + i + '" data-transition="slide" class="info-go">' + name.county + '</a></li>';
        $("#chipperList").empty();


    $("#chipperList").append(li).promise().done(function () {

        $(this).on("click", ".info-go", function (e) {
            e.preventDefault();
            $("#listDetails").data("info", info[this.id]);


            $.mobile.changePage("#listDetails");
        });


        $(this).listview("refresh");
    });
});
});
});



$(document).on("pagebeforeshow", "#listDetails", function () {

    var info = $(this).data("info");

    var info_view = "";
    for (var key in info) {
        info_view += '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar field" style="font-weight : bold; text-align: left;">' + key + '</div></div><div class="ui-block-b"><div class="ui-bar value" style="width : 70%">' + info[key] + '</div></div></div>';
    }

    $(this).find("[data-role=content]").html(info_view);
});

LIST_DATA.JSON

[{
        "county": "Antrim",
        "name": "Flash in The Pan",
        "address": "Main Street, Bushmills",
        "info": "Dummy Text"
    }, {
        "county": "Armagh",
        "name": "The Galley",
        "address": "Barrack Street, Armagh",
        "info": "Dummy Text"
    }, {
        "county": "Carlow",
        "name": "Roosters",
        "address": "Market Square, Bagenalstown",
        "info": "Dummy Text"
    }
]

【问题讨论】:

  • 你有这方面的javascript吗?
  • 除了&lt;script&gt; 标签中链接的文件外,没有 JavaScript。我应该有吗??
  • 如果还没有解决我可以帮你
  • 并非如此。我在第二页上取消了dialog,但我在该页面上使用了data-rel="back",这给出了同样的问题并且不得不使用data-ajax="false",这意味着我失去了页面转换
  • @香烟独角兽我没有得到你

标签: javascript jquery jquery-mobile dialog


【解决方案1】:

此代码将解决您的问题:

Home.html

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>List App</title>

    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <script src="../JS/jquery.mobile.icons-1.4.5.min.css"></script>


    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
    <link rel="stylesheet" href="../CSS/stylesheet.css"/>
    <link rel="stylesheet" href="../CSS/jquery.mobile.icons-1.4.5.min.css"/>
    <link rel="stylesheet" href="../CSS/jquery.mobile.inline-png-1.4.5.min.css"/>
    <script>
    $(document).on("pageinit", "#filterPage", function () {
    $.getJSON("../JSON/list_data.json", function(info){
    var li = "";
    $.each(info, function (i, name) {
        li = $('<li class="info-go"><a href="#" id="' + i + '" data-transition="slide" >' + name.county + '</a></li>');
        li.data("info",name);



    $("#chipperList").append(li);

    });
    $("#chipperList").listview("refresh");
    });
    });

     $(document).on("click", ".info-go", function (e) {

            $("#listDetails").data("info", $(this).data("info"));


            $.mobile.changePage("#listDetails");
        });



$(document).on("pagebeforeshow", "#listDetails", function () {

    var info = $(this).data("info");

    var info_view = "";
    for (var key in info) {
        info_view += '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar field" style="font-weight : bold; text-align: left;">' + key + '</div></div><div class="ui-block-b"><div class="ui-bar value" style="width : 70%">' + info[key] + '</div></div></div>';
    }

    $(this).find("[data-role=content]").html(info_view);
});
    </script>
    </head>

     <body>

<!--FIRST PAGE-->   
<div data-role="page" id="filterPage">

        <div data-role="header" data-theme="b">
             <h1>List App</h1>
       </div>

        <div data-role="content">
            <ul data-role="listview" id="chipperList" data-filter="true" data-filter-reveal="false" data-filter-placeholder="Chose a County..." data-divider-theme="a" data-inset="false" data-transition="slide" >
                <li data-role="list-divider" data-theme="b" role="heading" >County</li>
            </ul>
        </div>
 </div>  

<!--SECOND PAGE-->
        <div data-role="page" id="listDetails">
            <div data-role="header" data-theme="b">
             <h1>List App</h1>
             <div data-role="navbar">
              <a href="#filterPage" class="ui-btn ui-icon-search ui-btn-icon-left" ata-rel="back">Return to Search</a>
              </div>
            </div>

    <div data-role="content"></div>



        <div data-role="footer" data-theme="b" data-position="fixed">
            <div data-role ="navbar">    
                <ul>
        <li><a href="dialog.html" data-rel="dialog" data-transition="pop">Free Coupon</a></li> 


                </ul>
            </div>
            </div>


    </div> 

    </body>   

    </html>

dialog.html

 <div data-role="dialog" id="freeCoupon"> 
             <div data-role="header" data-theme="b"> 
             <h1>Congratulations!</h1>      
    </div>
             <div data-role ="content">
                <img src="../CSS/IMAGES/coupon.jpg"/>
             </div> 
         </div>

【讨论】:

    【解决方案2】:

    尝试从该行中删除“../”:

    <script src="../JS/jquery.mobile-1.4.5.min.js"></script>
    

    到这里:

     <script src="JS/jquery.mobile-1.4.5.min.js"></script>
    

    【讨论】:

    • 由于某种原因,如果我以这种方式键入文件,我的文本编辑器不会自动链接文件。
    • 嗯,我不知道,检查我的工作minimal reproducible example (jsfiddle.net/ejk8o1kb/3)。你的代码和这个不一样吗?
    • 我认为问题在于dialog 正在新页面上打开。它在第一页上运行良好。在问题中查看我更新的代码。
    • 如果我在第一页打开对话框,您的代码就可以工作。
    【解决方案3】:

    您可能需要将 autoOpen 设置为 false 并将单击事件处理程序应用于对话框,因为此问题听起来像是每次单击时都会重新创建对话框。

    尝试将其添加到页面中:

    $(document).ready(function() {
        $('#freeCoupon').dialog({
            autoOpen: false
        });
    
        $('#freeCoupon').click(function() {
            $('this').dialog('open');
        });
    });
    

    编辑:

    如果这不起作用,那么我会猜测 jQuery Mobile 文件没有正确链接。暂时,尝试使用 CDN URL 并替换:

    <script src="../JS/jquery.mobile-1.4.5.min.js"></script>
    <link rel="stylesheet" href="../CSS/jquery.mobile-1.4.5.min.css"/>
    

    与:

    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    

    【讨论】:

    • 不,它仍然闪烁打开然后关闭。我使用data-rel="back" 的后退按钮也是如此。
    • @carbon_unicorn 可能是文件链接不正确。尝试在我的答案中删除 JS 并查看我的编辑。
    • @carbon_unicorn 这很奇怪,因为它在我的机器上工作正常。您是否正在使用旧版浏览器或手机偶然查看此内容?
    • 我正在使用 Chrome、我的本地主机和 Ripple 手机模拟器。
    • 我已经相应地更新了我的代码,但我意识到代码在第一页上运行良好,但在第二页上却不行。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多