Creating a dialog JQuery Mobile

2020-02-12 00:37发布

I am trying to create a dialog with Jquery mobile. I have tried to refer to the accepted answer in this SO question but it didn't work for me.

Here is my code:

 <div data-role="page" id="first"> 
    <!-- Code -->  
    <div id = "dialog" data-rel="dialog">
        <div id = "errorText"></div>
        <button id = "closeDialog">OK</button>
    </div>
</div>

And here is the JS to make it (inside a function):

//Nothing checked. Cannot continue. Add error message to div
$('#errorText').html("You must check the checkbox next to \"I Agree\" to continue");
//Open Dialog
$('#dialog').dialog();

When the code to create the dialog is reached nothing happens. Suggestions?

5条回答
三岁会撩人
2楼-- · 2020-02-12 01:21

The dialog should be a separate page div which you can load via Ajax or include in your HTML. Here is an example.

<!DOCTYPE html>
<html>
    <head>
    <title>Page Title</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
</head>
<body>
<div data-role="page">
    <div data-role="header">
        <h1>Sample</h1>
    </div>
    <div data-role="content">
        <p></p>
        <p><a href="dialog.html" data-rel="dialog" data-role="button">Is this a question?</a></p>
    </div>
</div>
<div data-role="page" data-url="dialog.html">
    <div data-role="header">
        <h1>Dialog</h1>
    </div>
    <div data-role="content">
        <p>Is this an answer?</p>
    </div>
</div>
</body>
</html>
查看更多
beautiful°
3楼-- · 2020-02-12 01:24

Just this,

<div data-role="popup" id="popupDialog" data-overlay-theme="a">
                Hello world
 </div>

 $('#popupDialog' ).popup('open');
查看更多
家丑人穷心不美
4楼-- · 2020-02-12 01:35

This worked for me, from the "Local, internal linked "pages"" section of http://jquerymobile.com/demos/1.0b1/#/demos/1.0b1/docs/pages/docs-pages.html

http://jsfiddle.net/homer2/ra7Hv/

<div data-role="page" id="foo">
    <div data-role="header">
        <h1>
            Foo
        </h1>
    </div><!-- /header -->
    <div data-role="content">
        <p>
            I'm first in the source order so I'm shown as the page.
        </p>
        <p>
            View internal page called <a href="#bar" data-rel="dialog">bar</a>
        </p>
    </div><!-- /content -->
    <div data-role="footer">
        <h4>
            Page Footer
        </h4>
    </div><!-- /footer -->
</div><!-- /page -->

<!-- Start of second page -->
<div data-role="page" id="bar">
    <div data-role="header">
        <h1>
            Bar
        </h1>
    </div><!-- /header -->
    <div data-role="content">
        <p>
            I'm second in the source order so I'm not shown as the page initally.
        </p>
        <p>
            <a href="#foo">Back to foo</a>
        </p>
    </div><!-- /content -->
    <div data-role="footer">
        <h4>
            Page Footer
        </h4>
    </div><!-- /footer -->
</div><!-- /page -->
查看更多
Bombasti
5楼-- · 2020-02-12 01:41

I did a generic dialog which opens from JavaScript. I hope this will help you.

HTML code:

<div data-role="page" id="genericDialog">
    <div data-role="header" ><h3 id="genericDialogHeader"></h3></div>
    <div data-role="content" id="genericDialogContent"></div>
</div>

And JavaScript code:

function openDialog (title,body) {
    //Setting values
    $("#genericDialogHeader").html(title);
    $("#genericDialogContent").html(body);
    //Showing the generic dialog
    $.mobile.changePage( "#genericDialog", { role: "dialog" } );
}
查看更多
可以哭但决不认输i
6楼-- · 2020-02-12 01:44

You could use

$('#errorText').html("You must check the checkbox next to \"I Agree\" to continue");
$.mobile.changePage('dialog', 'slide', false, false);

More info on http://jquerymobile.com/demos/1.0b1pre/#/demos/1.0b1pre/docs/pages/docs-navmodel.html

查看更多
登录 后发表回答