js覆盖确认 [英] js override confirm
问题描述
我想尝试使用模态对话框覆盖window.confirm函数。
I want to try to override window.confirm function with modal dialog.
<a href="http://example.com" onClick="return confirm('you want to go?')">
<script>
window.confirm = function(message){
$("#confirm-dialog").modal('show');
$("#confirm-dialog .modal-body p").html(message);
$("#confirmYes").on("click", function () {
return true;
});
}
</script>
当我在#confirmYes元素上单击模态窗口时,它返回true,但是通过href链接重定向将无法工作...为什么?
When I click in modal window on the #confirmYes element it returns true, but the redirect by href link will not work...Why?
有人可以告诉我如何在不改变链接的情况下做这件事吗?
谢谢
Can somebody tell me how I can do this thing without changing my link? Thanks
UPD
Yii框架为CGridView小部件生成代码,我想覆盖它。我无法更改此代码,因为它在框架中。相反,这个确认标准我想使用我的模态窗口
UPD Yii framework generates that code for CGridView widget and i want to override it. I can't change this code, because its in framework. Instead this confirm standard i want to use my modal window
$(document).on('click','#product-grid a.delete',function() {
if(!confirm('Are you sure you want to delete this item?')) return false;
var th=this;
var afterDelete=function(){};
$.fn.yiiGridView.update('product-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(data) {
$.fn.yiiGridView.update('product-grid');
afterDelete(th,true,data);
},
error:function(XHR) {
return afterDelete(th,false,XHR);
}
});
return false;
});
推荐答案
这是我们在公司使用的一种做法一个UI转换项目。
Here is a practice that we used in my company in a UI conversion project.
虽然很难看,但效果还不错。
It is ugly though, but it works just fine.
var clickState={};
var justClicked=null;
window.confirm = function(message) {
var e = window.event || window.confirm.caller.arguments[0];
var el = e.target || e.srcElement; // the element's click that triggers confirm dialog
if(justClicked && clickState[justClicked]===true){
clickState[justClicked]=false;
return true;
}else{
// your async style confirmation dialog (e.g. jQuery's dialog)
showConfirmBox(message, function() {
justClicked=el;
clickState[el]=true;
$(justClicked).click(); // in the call back function , click the target again.
});
}
return false;
};
这篇关于js覆盖确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!