【发布时间】:2014-07-08 06:38:58
【问题描述】:
在我的 MVC 项目中,我有一个在新选项卡中打开页面的按钮。单击按钮时,页面将在新页面上打开,并且应禁用该按钮。当用户关闭选项卡时,我想启用该按钮。有什么办法可以在 jquery 或 javascript 中做到这一点?我已经搜索了网络但没有答案。
【问题讨论】:
标签: javascript jquery asp.net-mvc
在我的 MVC 项目中,我有一个在新选项卡中打开页面的按钮。单击按钮时,页面将在新页面上打开,并且应禁用该按钮。当用户关闭选项卡时,我想启用该按钮。有什么办法可以在 jquery 或 javascript 中做到这一点?我已经搜索了网络但没有答案。
【问题讨论】:
标签: javascript jquery asp.net-mvc
概念性的想法是在外部上下文(模块或全局变量)中维护对打开窗口的引用,以便可以轻松地从您的代码中访问它。示例如下:
var windowsArray = [];
function addWindowInTab() {
var newWindow = window.open('page.html', '_newtab');
windowsArray[windowsArray.length] = newWindow;
newWindow.onunload=enableButton;
}
function removeWindowTab(newWindow){
var idx = array.indexOf(newWindow);
if(idx != -1) {
windowsArray.splice(idx, 1);
}
}
function enableButton(){
$("#myButton").removeAttr("disabled");
}
//somewhere in MVC page
$(function(){
$("#buttonOpenWindow").click(function(){
addWindowInTab();
});
//later, when a button should be disabled...
if (windowsArray.length){
$("#myButton").attr("disabled", "disabled");
}
});
另请注意,您可以在此处执行一些更高级的检查,例如是否打开了具有特定名称的窗口或是否打开了多个窗口。请记住,为了打开一个新选项卡,必须调用 AddWindowInTab 函数以响应用户操作,例如按 Ctrl+单击鼠标左键
【讨论】: