简体   繁体   中英

Jquery confirm window not working on cancel button click

I have the following code in the javscript to open a confirmation dialog with yes and no options ,but i am facing some problem in opening the confirmation dialog.

function ConfirmDialog(obj, title, dialogText) {
    if (!dialogConfirmed) {
        $('body').append(String.Format("<div id='dialog' title='{0}'><p>{1}</p></div>",
                    title, dialogText));

        $('#dialog').dialog
                ({
                    height: 150,
                    modal: true,
                    resizable: false,
                    draggable: false,

                    close: function (event, ui) { $('body').find('#dialog').remove(); },
                    buttons:
                    {
                        'Yes': function () {
                            $(this).dialog('close');
                            dialogConfirmed = true;
                            GetRadWindow().close();
                            if (obj) obj.click();
                        },
                        'No': function () {
                            $(this).dialog('close');
                        }
                    }
                });
    }

    return dialogConfirmed;
}

This is my code on the aspx page

 <script type="text/javascript" src="Scripts/jquery-1.4.2.min.js"></script>
                <script type="text/javascript" src="Scripts/jquery-ui-1.8.2.custom.min.js"></script>
                 <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>

            <input id="Button2" type="button" value="Cancel" onclick="return ConfirmDialog(this, 'Confirmation', 'Are you sure want to close the window?');" />

could it be that you are including two versions of jquery in your page 1.4.1 and 1.4.2? Please change that and see.

Also String.Format in javascript? is that some method that you have written? Use sprintf instead.

This code reaches Line 3 and sees String.Format inside JavaScript and stops execution there.

There is no String.Format in jQuery / JavaScript. If you are particular about using String.Format in JavaScript , create one yourself.

String.prototype.format = function() {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
};

And use it like

$('body').append("<div id='dialog' title='{0}'><p>{1}</p></div>".format(title, dialogText));

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