简体   繁体   English

弹出jQuery对话框

[英]pop up jquery dialog box

hey frenz I have a problem with the jquery-ui dialog box. 嘿frenz我的jquery-ui对话框有问题。 The problem is that when I close the dialog box and then I click on the link that triggers it, it does not pop-up again unless i refresh the page or save any data in the dialog box. 问题是,当我关闭对话框然后单击触发它的链接时,除非刷新页面或在对话框中保存任何数据,否则它不会再次弹出。

Code i used is: 我使用的代码是:

<script type="text/javascript"> 

    $(document).ready(function () {
        $(".openDialog").live("click", function (e) {
            e.preventDefault();

            $("<div></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        close: function () {
                            $(this).remove();
                        },
                        modal: true
                    })
                    .load(this.href);
        });

        $(".close").live("click", function (e) {
            e.preventDefault();
            $(this).closest(".dialog").dialog("close");
        });
    });
</script>

I was able to get it to work by injecting another div with an hourglass/spinning-wheel "Please Wait" loading image. 通过向另一个div注入沙漏/旋转轮“请稍候”加载图像,我能够使其工作。 (I'm using jquery-1.7.1) (我正在使用jquery-1.7.1)

Try replacing this line: 尝试替换此行:

$("<div></div>")

With this: 有了这个:

$("<div><div style='text-align: center'><img src='@Url.Content("~/Content/images/loading.gif")' alt='Please Wait...' width='100px'/></div></div>")

This is my working code (exactly as the original post, except for the one line): 这是我的工作代码(与原始帖子完全一样,除了一行内容):

// these are for the popup dialogs
// need to use live instead of click because object doesnt exist on ready and will give an objectexpected
$(".openDialog, .editDialog").live("click", function(e) {
    e.preventDefault();

    // this div is duplicate of 'loading' div below
    $("<div><div style='text-align: center'><img src='@Url.Content("~/Content/images/loading.gif")' alt='Please Wait...' width='100px'/></div></div>")
        .addClass("dialog")
        .attr("id", $(this).attr("data-dialog-id"))
        .appendTo("body")
        .dialog({
            // NOTE: This is where the size of the popup window is set
            width: 800,
            position: 'top',
            title: $(this).attr("data-dialog-title"),
            close: function() { $(this).remove(); },
            modal: true
        })
        .load(this.href);
});
  1. What @MattBradley said. @MattBradley说什么。
  2. In this line, .attr("id", $(this).attr("data-dialog-id")) , you'r trying to grab the new <div> 's data-dialog-id attribute an assigning the new <div> 's an id attribute. .attr("id", $(this).attr("data-dialog-id")) ,您尝试获取新的<div>data-dialog-id属性,并为其分配一个新的<div>id属性。 I'm pretty sure you meant to assign the link's data-dialog-id attribute to the new <div> . 我很确定您打算将链接的data-dialog-id属性分配给新的<div>
  3. close: function() { $(this).remove(); } close: function() { $(this).remove(); } is redundant. close: function() { $(this).remove(); }是多余的。 Unless you meant "remove the link", not the dialog itself, in which case you would use link instead of this (see fiddle ). 除非您的意思是“删除链接”,否则不是对话框本身,在这种情况下,您将使用link代替this link (请参阅fiddle )。
  4. "As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()." “从jQuery 1.7开始,不推荐使用.live()方法。请使用.on()附加事件处理程序。jQuery较早版本的用户应优先使用.delegate()而不是.live()。”
    Taken from jQuery's live() documentation. 取自jQuery的live()文档。

$('body').on('click', '.openDialog', function (e) {
    e.preventDefault();

    var link = $(this);

    $("<div></div>")
        .addClass("dialog")
        .attr("id", $(link).attr("data-dialog-id"))
        .appendTo("body")
        .dialog({
            modal: true
        })
        .load($(link).attr('href'),  {
            html: "<p>Text echoed back to request</p>"
        });
});

$('body').on("click", '.close', function (e) {
    e.preventDefault();
    $(this).closest(".dialog").dialog("close");
});

Demo here: http://jsfiddle.net/Ag6wj/ 此处的演示: http : //jsfiddle.net/Ag6wj/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM