简体   繁体   English

在Jquery Modal对话框中单击OK按钮时触发事件

[英]Triggering event on clicking OK button in Jquery Modal dialog box

I am trying to display a dialog box with just an OK button on response of an ajax call. 我正在尝试显示一个对话框,只有一个OK按钮响应ajax调用。 When the user clicks OK, it should reload the page. 当用户单击“确定”时,它应该重新加载页面。 But now page reload is immediately happening after the dialog box is popped up. 但是现在弹出对话框后会立即重新加载页面。 It is not waiting for the user to click OK. 它不是在等待用户单击“确定”。 FYI I am using Jquery Modal dialog box. 仅供参考我正在使用Jquery Modal对话框。

Simple browser alert() does the job for me, but I don't like the appearance of alert(). 简单的浏览器警报()为我做的工作,但我不喜欢alert()的外观。

Any help is highly appreciated! 任何帮助都非常感谢!

$.ajax({
    url: "modules/mymod/save.php", 
    type: "POST",
    data: $('#requestForm').serialize(),
    statusCode: {404: function () {alert('page not found');}},
    success: function (data) {
        // alert(data);
        modal({type: 'alert', title: 'Alert', text: data});
        window.location.href = window.location.href;
    }
});

You can pass the dialog modal buttons attributes, each with a registered event, like this: 您可以传递dialog模态buttons属性,每个属性都有一个已注册的事件,如下所示:

$.ajax({
    url: "modules/mymod/save.php", 
    type: "POST",
    data: $('#requestForm').serialize(),
    statusCode: {404: function () {alert('page not found');}},
    success: function (data) {

        $("#dialog-confirm").dialog({
          resizable: false,
          height: 200,
          modal: true,
          buttons: {
              Proceed: function() {
                 window.location.href = window.location.href;
              },
              Cancel: function() {
                 // Cancellation code here
              }
           }
       });
    }
});

Because your reload runs irrespectively of what is clicked. 因为您的重新加载与所点击的内容无关。 If you want to assign a callback function to the modal window: 如果要将回调函数分配给模态窗口:

jQuery UI dialog with boolean return - true or false 带有布尔返回的jQuery UI对话框 - true或false

Also, there is no need to make location.href equal itself (or use the window object). 此外,不需要使location.href本身相等(或使用窗口对象)。 location.reload() works just as well. location.reload()可以正常工作。

Simple browser alert() does the job for me because alert() is an blocking call. 简单的浏览器警报()为我完成工作,因为alert()是一个阻塞调用。 If you omit the alert then your code is not bind with any event to check whether user clicked on a button or not, that's why the code block executes immediately and page reloads. 如果省略警报,那么您的代码不会与任何事件绑定,以检查用户是否单击了按钮,这就是代码块立即执行和页面重新加载的原因。

So bind the following code: 所以绑定以下代码:

window.location.href = window.location.href;

inside some button click, to resolve the issue. 在一些按钮内单击,以解决问题。

Reference: 参考:

            $.ajax({
                url: "modules/mymod/save.php", 
                type: "POST",
                data: $('#requestForm').serialize(),
                statusCode: {404: function () {alert('page not found');}},
                success: function (data) {
                    // alert(data);
                    modal({
                        type: 'alert',
                        title: 'Alert',
                        text: data,
                        buttons: [{
                                text: 'OK', //Button Text
                                val: 'ok', //Button Value
                                eKey: true, //Enter Keypress
                                addClass: 'btn-light-blue btn-square', //Button Classes 
                                onClick: function() {
                                    window.location.href = window.location.href;
                                }
                            }, ],
                        center: true, //Center Modal Box?
                        autoclose: false, //Auto Close Modal Box?
                        callback: null, //Callback Function after close Modal (ex: function(result){alert(result);})
                        onShow: function(r) {
                            console.log(r);
                        }, //After show Modal function
                        closeClick: true, //Close Modal on click near the box
                        closable: true, //If Modal is closable
                        theme: 'xenon', //Modal Custom Theme
                        animate: true, //Slide animation
                        background: 'rgba(0,0,0,0.35)', //Background Color, it can be null
                        zIndex: 1050, //z-index
                        buttonText: {
                        ok: 'OK',
                        yes: 'Yes',
                        cancel: 'Cancel'
                    },
                    template: '<div class="modal-box"><div class="modal-inner"><div class="modal-title"><a class="modal-close-btn"></a></div><div class="modal-text"></div><div class="modal-buttons"></div></div></div>',
                    _classes: {
                    box: '.modal-box',
                    boxInner: ".modal-inner",
                    title: '.modal-title',
                    content: '.modal-text',
                    buttons: '.modal-buttons',
                    closebtn: '.modal-close-btn'
                      }
                    });
                   }
            });

dont use window.location function in success.instead open the modal with ok button at success(how to do that, I think you know already) and assign some id to that button let say id="loction_btn" . 不要成功使用window.location函数。在成功时用ok按钮打开模态(怎么做,我想你已经知道了)并为该按钮指定一些id,比如id="loction_btn" then use just this 然后使用这个

$('document').on('click','#location_btn',function(){
   window.location.href = window.location.href;
});

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

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