使用 Telerik 控件在 Javascript 中进行退出确认
我在我的项目中使用 Telerik 控件。在我的页面上,有一些指向另一个页面的链接、几个 radtextboxes 和一个 radbutton(它会导致回发)。当文本框值更改时,该按钮将变为启用状态。然后,在我的 window.onbeforeunload
中,我检查该按钮是启用还是禁用。如果启用,则会出现确认对话框。代码如下所示:
window.onbeforeunload = function (evt) {
var ClientGeneral_btnSave = $find('<%=btnSave.ClientID %>');
if (ClientGeneral_btnSave.get_enabled() == true) {
var message = 'You will lose unsaved data?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
这段代码运行良好。当我关闭选项卡时,会出现确认对话框。当我点击链接时,它确实如此。但是,当我单击 btnSave 本身时,也会出现该对话框,这是不寻常的。我希望 btnSave 不会导致 onbeforeunload
事件
请告诉我如何执行此操作。
先感谢您
I'm using Telerik controls for my project. On my page, there are some links to another page, several radtextboxes and a radbutton (it causes a postback). When the textbox values are changed, the button becomes enabled. Then, in my window.onbeforeunload
, I check if the button is enabled or disabled. If it is enabled, then the confirm dialog appears. The code looks like this :
window.onbeforeunload = function (evt) {
var ClientGeneral_btnSave = $find('<%=btnSave.ClientID %>');
if (ClientGeneral_btnSave.get_enabled() == true) {
var message = 'You will lose unsaved data?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
This code works well. When I close the tab, the confirm dialog appears. When I click on the links, it does. But, when I click on the btnSave itself, the dialog appears too, which is unsual. I want the btnSave NOT to cause onbeforeunload
event
Please tell me how to do this.
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 btnSave 回发看起来确实如此,它将在 onbeforeunload 上触发。因此,您有几个选择
如果您不需要,则阻止 btnSave 回发。最简单的方法是将此属性放在 asp:Button 标记中
OnClientClick="return false"
连接 javaScript/jQuery 方法以禁用 onbeforeunload 事件。我之前通过在隐藏字段中存储一个值并使用它来表示应触发 onbeforeunload 事件来完成此操作。
例如
,更改您的 on beforeunload 处理程序以检查隐藏字段是否不等于 1(将 0 设置为默认值,即
您可以通过使用 JQuery 在 btnSave 点击事件中取消绑定 onbeforeunload 处理程序而不是使用隐藏字段来做得更好 。
选项 2 可能会变得很繁琐 - 所以祝你好运
It will fire onbeforeunload this if the btnSave posts back which it looks as if it is. Therefore you have a couple of choices
Prevent the btnSave from posting back if you don't need it to. Easiest way to do it is put this attribute in the asp:Button markup
OnClientClick="return false"
Wire up a javaScript/jQuery method to disable the onbeforeunload event. I did this before by stashing an value in a hidden field and using this to signal that the onbeforeunload event should fire.
for instance
and change your on beforeunload handler to check that the hidden field is not equal to 1 (make 0 the default i.e.
You could probably do something better by unbinding the onbeforeunload handler in the btnSave click event using JQuery rather than using a hidden field to override.
Option 2 can get fiddly though - so best of luck