如何在使用jquery对话框时阻止基页重新加载

how to stop base page from reloading when using jquery dialog

本文关键字:新加载 基页 加载 jquery 对话框      更新时间:2023-09-26

我在我的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)重新加载(或什么似乎是像一个页面重新加载),以便当我关闭对话框,我看到我点击的记录,而不是不得不再次向下滚动。

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

<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元素,并为打开事件指定一个回调,该回调会动态加载页面' Sample.htm '的内容。

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

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

就是这样!