代码之家  ›  专栏  ›  技术社区  ›  Suresh

javascript确认操作

  •  0
  • Suresh  · 技术社区  · 15 年前

    我有一个页面有一个继续按钮。如果单击“继续”,则 如果单击“确定”弹出窗口,则上述3个弹出窗口都会依次出现,但如果单击弹出窗口上的“取消”,则页面应该关闭,这是由我的 closeAction(); 方法。

    如果在第一个或第二个弹出窗口中单击“取消”,则该页不会关闭,但第三个可以工作。

    if(document.getElementById(id1).value){
        if(!confirm("click Cancel to Close page and ok to go to next popup")){
             closeAction();
        }
           }
    
    if(document.getElementById(id2).value){        
           if(!confirm("click Cancel to Close page and ok to go to next popup")){
            closeAction();
        }
           }
    if(!confirm(" to be made click 'Cancel'")){
            closeAction();
             }
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Guffa    15 年前

    使用else,以便在调用closeAction方法后跳过其余检查:

    if (document.getElementById(id1).value && !confirm("click Cancel to Close page and ok to go to next popup")) {
       closeAction();
    } else if (document.getElementById(id2).value && !confirm("click Cancel to Close page and ok to go to next popup")) {
       closeAction();
    } else if (!confirm(" to be made click 'Cancel'")) {
       closeAction();
    }
    
        2
  •  0
  •   Anthony Mills    15 年前

    记得, && 是否短路:

    if (document.getElementById(id1).value &&
        !confirm("click Cancel to Close page and ok to go to next popup") &&
        document.getElementById(id2).value &&
        !confirm("click Cancel to Close page and ok to go to next popup") &&
        !confirm(" to be made click 'Cancel'"))
    {
        closeAction();
    }