覆盖javascript确认框 [英] Override javascript confirm box

查看:72
本文介绍了覆盖javascript确认框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SweetAlert覆盖javascript确认框。我也研究了这个,但我找不到合适的解决方案。



我正在使用这样的确认



  if (确认(' 是否要删除此评估')==  true ){
// 某事
}
其他 {
//
}



我正在使用它来覆盖



  window  .confirm =  function (data,title,okAction){
swal({
title: ,text:data,type: warning,showCancelButton: true ,confirmButtonColor: #DD6B55,confirmButtonText: ,cancelButtonText: ,closeOnConfirm: true ,closeOnCancel: true
}, function ( isConfirm){
if (isConfirm)
{
okAction();
}
});
// return proxied.apply(this,arguments);
};



现在确认框被sweetalert取代。当用户单击Yes按钮时,应调用OK操作确认框。但这并不是在调用



并且在上面的代码中发生了一个错误Uncaught TypeError:okAction不是一个函数。



请建议我,我应该为覆盖确认框做。

解决方案

严格来说,JavaScript中没有覆盖这样的概念;即使你所做的事情类似于OOP覆盖,但JavaScript甚至不接近OOP。做这些事情没有多大意义。现在,自然地,参数发生了什么取决于函数的调用方式。如果你的 window.confirm 对象被调用,因为原来的函数应该被使用,那么只有一个参数,所以 onAction 根本就不存在。当然,你不能调用未定义的对象,也不能调用非函数对象。



我会忘记 window.confirm的想法。它几乎不是为优质精炼代码而设计的。出于这样的目的,您可以更好地使用灵活的jQuery对话框:

https://jqueryui.com/dialog [ ^ ]。



重要的是,通过这种模拟模态行为,您可以在单个Web页面上执行所有操作,可以保持一致的样式,等等。



-SA

I am trying to override javascript confirm box with SweetAlert. I have researched also about this but I can't found proper solution.

I am using confirm like this

if (confirm('Do you want to remove this assessment') == true) {
   //something
}
else {
   //something
}


And I am using this for overriding

window.confirm = function (data, title, okAction) {
                swal({
                    title: "", text: data, type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes", cancelButtonText: "No", closeOnConfirm: true, closeOnCancel: true
                }, function (isConfirm) {
                    if (isConfirm)
                    {
                        okAction();
                    }
                });
                // return proxied.apply(this, arguments);
            };


Now confirm box is replaced with sweetalert. When user click on Yes button then OK action of confirm box should be called. But this isn't calling

And In above code an error occurred Uncaught TypeError: okAction is not a function.

Please suggest me I should I do for override confirm box.

解决方案

Strictly speaking, there is no such concept as "override" in JavaScript; even though what you do resembles OOP overriding, but JavaScript is not even close to OOP. Doing such things make little sense. Now, naturally, what happens to the argument depends on how the function is called. If your window.confirm object is called as the original function is supposed to be used, there is only one argument, so onAction simply does not exist. Naturally, you cannot call undefined object, and you cannot call a non-function object.

I would just forget the idea of window.confirm. It is hardly designed for quality refined code. For such purposes, you can better use flexible jQuery dialog:
https://jqueryui.com/dialog[^].

Importantly, with such emulated modal behavior, you do everything on a single Web page, can keep consistent styles, and so on.

—SA


这篇关于覆盖javascript确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆