开发者

Issue with jquery mobile dialog popup when dialog link is dynamically generated

I am using JQuery Mobile and setting up the dialog box link dynamically. Upon user clicking the link, the dialog opens in a separate page, NOT in a dialog box popup. I have the working code below. Can somebody please tell me what I am doing wrong?

Thanks in advance,

Vish

Here is a link to this sample on jsfiddle http://jsfiddle.net/vishr/ekLWd/

When user clicks on Add Item, I dynamically change the text to Remove Item and when user clicks Remove Item, I put up a dialog box, which in this case opens in a separate page, not a dialog pop-up

<!DOCTYPE html> 
<html> 
<head> 
<title>Page Title</title> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js">    </script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>
<script type="text/javascript">

(function(){
$(document).ready(function() {
    $("#del-item").live('click', function(){  
    $.mobile.changePage($('#del-dialog'), 'pop');
    });    
    $("#add-item").live('click', function(){  
        $('#add-item .ui-btn-text').text('Remove Item');
        $('.item').attr('id', 'del-item');
        $('.item').attr('href', '#del-dialog');
        $('.item').attr('data-rel', 'dialog');
    });    
    $("#close-dialog").click(function(){      
        $('.ui-dialog').dialog('close');
    });
 });
})();
</script>
</head> 
<body> 

<div data-role="page">

<div data-role="header">
<h1>Page Title</h1>
</div><!-- /header -->

<div data-role="content">
      <div id="dialog-container" data-inline="true">
        <a href="#" class="item" id="add-item" data-role="button" data-inline="true">Add Item</a>  
      </div>   
</div><!-- /content -->

<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
  <div data-role="dialog" role="dialog" id="del-dialog">
  <div data-role="content">
       开发者_Python百科    <div style="text-align:center;"><strong>Remove Item?</strong></div>
            <form id="myForm">
                <div data-role="fieldcontain">
                    <fieldset class="ui-grid-a">
                        <div class="ui-block-a"><a href="#" id="close-dialog" data-theme="a" data-rel="back" data-role="button">Cancel</a></div>
                        <div class="ui-block-b"><a href="#" id="close-dialog" data-theme="a" type="submit" data-role="button">OK</a></div>

                    </fieldset>
                </div>
            </form>
        </div>
  </div>

 </body>
 </html>


If you want the dialog box to open in the same page then you should use SimpleDialog plugin available for jquery mobile.

Below is your code which i have customized using SimpleDialog plugin so that the dialog box appears in the same page

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.js"></script>
<script type="text/javascript" src="http://dev.jtsage.com/cdn/simpledialog/latest/jquery.mobile.simpledialog.min.js"></script>
<script type="text/javascript">

 (function(){
     $(document).ready(function() {
         $("#del-item").live('click', function(){  
         //$.mobile.changePage($('#del-dialog'), 'pop');

                     $(this).simpledialog({

                        'prompt' : 'What do you say?',
                        'cleanOnClose': true,
                        'buttons' : {
                            'OK': {
                                click: function () {
                                    $('#simplestringout').text($('#simplestring').attr('data-string'));
                                }
                            },
                            'Cancel': {
                                click: function () { console.log(this); },
                                icon: "delete",
                                theme: "c"
                            }
                        }
                    })
            });    
         $("#add-item").live('click', function(){  
             $('#add-item .ui-btn-text').text('Remove Item');
             $('.item').attr('id', 'del-item');
             $('.item').attr('href', '#del-dialog');
             $('.item').attr('data-rel', 'dialog');
         });    
         $("#close-dialog").click(function(){      
             $('.ui-dialog').dialog('close');
         });
      });
     })();
 </script>
<title>Page Title</title>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Page Title</h1>
</div><!-- /header -->
<div data-role="content">
      <div id="dialog-container" data-inline="true">
        <a href="#" class="item" id="add-item" data-role="button" data-inline="true">Add Item</a>  
      </div>   
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
 </body>
 </html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜