简体   繁体   中英

jquery dialog add button after ajax call

I have a dialog window that has some confirmation stuff in it as the result of a form submit:

 $('#newUserDialog').dialog({
        autoOpen: false,
        width: 600,
        modal: true,
        resizable: false,
        close: function() {
            window.location.reload(true);
        },
        buttons: {
            "Complete": function() {
                    $.ajax({
                            type: "POST",  
                            url: "checkData.php",
                            data: formData+"&complete=1",
                            success: function(result){
                            alert(result);
                            $('#newUserDialog').html('User has been built');
                            }
                    });
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });

when the user clicks complete, it fires off the ajax call and does checkdata.php stuff. when that is complete I want to remove "Complete" and "Cancel" and add a button "Continue". The continue button will reload the page. how can i accomplish this?

currently in this code, it works as desired but if the user doesn't like what he sees in confirmation window before he clicks Complete, if he clicks cancel, it reloads page. I only want to reload page after the ajax call.

You can use following for adding new button in ajax callback;

var buttonSet = $('#newUserDialog').parent().find('.ui-dialog-buttonset');
var newButton = $('<button>Continue</button>');
newButton.button().click(function () {
    window.location.reload(true);
});
buttonSet.html(newButton); 

You can see demo here: jsfiddle

 <script>
  var myModalExample;
  $(function() {


    myModalExample = $( "#newUserDialog" ).dialog({
        autoOpen: true,
        width: 600,
        modal: true,
        resizable: false,
        close: function() {
            window.location.reload(true);
        },
        buttons: {
            "Complete": function() {
                    $.ajax({
                            type: "POST",  
                            url: "checkData.php",
                            data: formData+"&complete=1",
                            success: function(result){

                              myModalExample.dialog({ buttons: [ { text: "Continue", click: function() { $( this ).dialog( "close" ); } } ] });

                            }
                    });
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });

  });
  </script>

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