【问题标题】:How to disable button when corresponding page is opened in new tab在新标签页中打开相应页面时如何禁用按钮
【发布时间】:2014-07-08 06:38:58
【问题描述】:

在我的 MVC 项目中,我有一个在新选项卡中打开页面的按钮。单击按钮时,页面将在新页面上打开,并且应禁用该按钮。当用户关闭选项卡时,我想启用该按钮。有什么办法可以在 jquery 或 javascript 中做到这一点?我已经搜索了网络但没有答案。

【问题讨论】:

    标签: javascript jquery asp.net-mvc


    【解决方案1】:

    概念性的想法是在外部上下文(模块或全局变量)中维护对打开窗口的引用,以便可以轻松地从您的代码中访问它。示例如下:

    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+单击鼠标左键

    【讨论】:

    • 感谢 Borys,但如何启用和禁用相应选项卡上的按钮打开和关闭
    猜你喜欢
    • 2019-12-28
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多