我有一个在服务器上执行并更改页面的LinkButton
。从历史上看,我有一个 confirm
消息框,它执行 OnClientClick
以确保用户想要离开。
到目前为止,它看起来像这样:
ASP.NET:
<asp:LinkButton runat="server" ID="ChangePage" Text="Change page"
OnClientClick="confirm('are you sure you want to change page?');"
OnClick="Navigate" >
Change Page
</asp:LinkButton>
HTML 输出:
<a id="MainContent_ChangePage"
onclick="confirm('are you sure you want to change page?');"
href="javascript:__doPostBack('ctl00$MainContent$ChangePage','')" >
Change page
</a>
这样一切都可以正常工作。问题是我试图用更漂亮的 jQuery-UI 实现替换所有确认框,如下所示:
window.confirm = function (message, obj) {
$('<div/>')
.attr({ title: 'Webpage Confirm'})
.html(message)
.dialog({
resizable: false,
modal: true,
width: 500,
buttons: {
"OK": function () {
__doPostBack(obj, '');
$(this).dialog('close');
return true;
},
"Cancel": function () {
$(this).dialog('close');
return false;
}
}
});
};
我相信这与以下事实有关:confirm
对话框同步运行,而 jQuery 对话框异步发生。但是,我认为设置 modal: true
会导致它等待响应。
如何重写 window.confirm
以获得一致的行为?
最佳答案
将此添加到“确定”操作:
var href = $(obj).attr("href");
window.location.replace(href);
window.navigator.location(href);
return true;
并删除 __postback 行
它适合我
关于javascript - 使用自定义 jQuery 防止回发确认在 asp 上实现 :LinkButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22518456/