开发者

How to create jQuery Dialog in function

Does anyone know how to create a jQuery Dialog in a function? I can't find an attribute to set the message... In every example I found, the dialog has been statically written into the code in a div-tag. However, I want to create it dinamically, so I need to know how to create a dialog in a function.

It is no problem to set the title:

    <script>
    // increase the default animation speed to exaggerate the effect
    $.fx.speeds._default = 1000;
    $(function() {
        $( "#dialog" ).dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode"
 开发者_高级运维       });

        $( "#opener" ).click(function() {
            //$( "#dialog" ).dialog( "open" );
            $( this ).dialog({ title: 'Please confirm deletion!' });
            return false;
        });
    });
    </script>
</head>
<body>

I have the documentation and some examples here.

Thanks for helping out guys.

Cheers, doonot

============================= [SOLUTION]=====================================

Thanks for all who answered this questions. This is how i wanted it:

    function createDialog(title, text) {
    return $("<div class='dialog' title='" + title + "'><p>" + text + "</p></div>")
    .dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Confirm": function() {
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

And it can be called for example like this (clicking on an image):

<img src="delete.png" onClick="createDialog('Confirm deletion!', 'Do you really want to delete this package?')">


function createDialog(title, text, options) {
    return $("<div class='dialog' title='" + title + "'><p>" + text + "</p></div>")
    .dialog(options);
}


Here is a simple example:

function openDialog(message) {
    if ($('#dialog').length == 0) {
        $(document.body).append('<div id="dialog">'+message+'</div>');
    } else {
        $('#dialog').html(message);
    }
    $( "#dialog" ).dialog({
        autoOpen: false,
        show: "blind",
        hide: "explode"
    });
    $( "#dialog" ).dialog("open");
}


I used this with additionally jQuery tmpl plugin.

var confirmTemplate = jQuery.template("<div class='dialog' title='${title}'><p>${text}</p></div>");

   function showDialog(options) {
        if (options && options.data && options.dialog) {
            var dialogOptions = jQuery.extend({}, { modal: true, resizable: false, draggable: false }, options.dialog);
            return jQuery.tmpl(confirmTemplate, options.data).dialog(dialogOptions);
        }
    }

    function hideDialog (item) {
        if (!item.jQuery) item = $(item);
        item.dialog("close").dialog("destroy").remove();

}

usage:

showDialog({
             data: {
                      title: "My Title",
                      text: "my Text"
                   }
             dialog: {
                     myDialog: "options"
             }
           });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜