简体   繁体   中英

Add a dialog using jQuery using html generated by jQuery

I want to add a jQuery dialog using an string containing html code stored in a variable. Is it possible? Here is some of the tried code .

 $("#remove-post").dialog({
     autoOpen: false,
     height: 'auto',
     width: 'auto',
     modal: true
 });
 $("body").delegate("a.delete-post", "click", function (e) {
     e.preventDefault();
     button = $(this);
     remove_dialog_html = '<div id="remove-post">Are you sure you want to delete this post? Once, deleted it can\'t be reversed.</div>';
     $('#remove-post').dialog("open");
 });

You can simply change the html of this element.

$('#remove-post').html('<div id="remove-post">Are you sure you want to delete this post? Once, deleted it can\'t be reversed.</div>');

jsFiddle Demo

Edit:

You can also avoid adding the dialog to the original HTML file, by creating and destroying it when you open and close the dialog.

 $('<div></div>')
        .appendTo('body')
        .html(htmlContent)
        .dialog({
            autoOpen: true,
            height: 'auto',
            width: 'auto',
            modal: true,

            close: function (event, ui) {
                $(this).remove();
            }
        });

jsFiddle Demo

Please Refer this link link

Thanks!!

Edit: Try this:

$(document).ready(function () {
    $("#divModifyDatesDialog").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        draggable: false,
        position: "center",
        width: 550,
        buttons: {
            "Yes": {
                click: function () {
                -------------
            },
            "No": {
                click: function () {
                    ------
                }
            }
        }
    }); 

You cannot initialize jQuery dialog like this since it is not in the DOM at the page load time (where jQuery initialize the stuff). What you have to do is that initialize dialog after adding the html to the DOM. Just before $('#remove-post').dialog("open");

Are you looking for something like this. Check the fiddle below. Changed the code as per your requirement if this is what you are looking for.

http://jsfiddle.net/8R7xA/1/

$("#remove-post").dialog({
      autoOpen: false,
      height:'auto', 
      width:'auto',
      modal: true
});

$(document).ready(function(){

   var remove_dialog_html= '<div id="remove-post">Are you sure you want to delete this post? Once, deleted it can\'t be reversed.</div>';
   $('#remove-post').html(remove_dialog_html);
   $('#remove-post').dialog("open");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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