【问题标题】:how to stop base page from reloading when using jquery dialog使用jquery对话框时如何停止重新加载基本页面
【发布时间】:2012-12-28 01:09:16
【问题描述】:

我的 editRecords.php 页面上有以下代码。此页面是一个记录表,当我单击视图链接时,它会打开一个对话框,其中包含 displayRecord.php 页面。问题是,如果我打开表格中的最后一条记录,而不是打开/关闭对话框,并且 editRecords.php 页面保持原样,它似乎会重新加载,这会将我带回页面顶部。

$(document).ready(function() { 

     //creating a dialog box
     var dlg=$('#ticketDetails').dialog({
        title: 'Ticket Details',
        resizable: false,
        autoOpen:false,
        modal: true,
        hide: 'fade',
        width: 1300

     });

    //loading dialog box with record
    $('a.view').click(

    function(e) 
    {
         dlg.load('displayRecord.php?id='+this.id, function(){ 
         dlg.dialog('open');
         });

      });

});

我曾尝试使用e.preventDefault(),但这会导致对话框加载时焦点位于中间而不是顶部。

function(e) 
        {
                 //tested here e.preventDefault();
             dlg.load('displayRecord.php?id='+this.id, function(){ 
             dlg.dialog('open');
             });
         //tested here e.preventDefault();

如何修复/调整这种行为?

谢谢。

澄清: e.preventDefault() 有效,但问题是它会导致对话框加载时焦点位于中间。我打开或关闭对话框没有问题。我只想停止重新加载基本页面(editRecords.php)(或看起来像是重新加载页面),这样当我关闭对话框时,我会看到我点击的记录,而不必再次向下滚动。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    让我们看看如何在这个对话框中动态加载内容

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Load Page Dynamically inside a jQuery UI Dialog</title>
    <link rel="Stylesheet" type="text/css" href="
    http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css
    " />
    <script  type="text/javascript" src="
    http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
    </script>
    <script type="text/javascript" src="
    http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js">
    </script>
    
    <script type="text/javascript">
        $(function ()    {
            $('<div>').dialog({
                modal: true,
                open: function ()
                {
                    $(this).load('Sample.htm');
                },         
                height: 400,
                width: 400,
                title: 'Dynamically Loaded Page'
            });
        });
    </script>
    </head>
    <body>
    
    </body>
    </html>
    

    如您所见,我们正在动态创建一个 div 元素并为 open 事件指定回调,该回调会动态加载页面“Sample.htm”的内容。

    多次打开和关闭对话框不会从页面中删除对话框。如果要实现关闭事件,请在对话框选项中使用此代码(未经测试的代码):

    close: function(e, i) { $(this).close(); }

    就是这样!

    【讨论】:

    • 谢谢你,但也许我的问题不清楚。见更新! : ]
    猜你喜欢
    • 2014-04-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多