【问题标题】:Cancel button not working for the confirm window取消按钮不适用于确认窗口
【发布时间】:2013-12-30 02:32:39
【问题描述】:

我正在尝试验证 JavaScript 中的单选按钮,这是我的代码:

if (document.ExamEntry.GCSE.checked == true) {
    confirm("You have selected GCSE. Is this correct?");
}
if (document.ExamEntry.AS.checked == true) {
    confirm("You have selected AS. Is this correct?");
}
if (document.ExamEntry.A2.checked == true) {
    confirm("You have selected A2. Is this correct?");
}

显示确认窗口并成功单击“提交”将您带到下一页,但是取消按钮似乎不起作用 - 当我希望它留在页面上时它只会将您带到下一页所以你可以改变你的选择。

我已经尝试过诸如返回结果之类的东西; 结果=假

它们要么不起作用,要么如果起作用,则反之亦然,因此取消按钮通过保持在同一页面上而起作用,但提交按钮也会发生这种情况。

【问题讨论】:

  • 下一页是什么?您是在谈论下一个确认框,还是在重定向某个地方?
  • 您的确认不会对确认的结果做任何事情。它们也可能是警报。
  • 您需要跟踪确认的结果并对其进行处理...
  • 向我们展示您的更多代码。
  • 你必须使用 if(confirm("You have selected GCSE. is correct?")) { //do something }

标签: javascript jquery radio-button confirm validating


【解决方案1】:

查看confirm 上的文档。它说,

result 是一个布尔值,表示选择了 OK 还是 Cancel(true 表示 OK)

这意味着你的每一行都应该检查返回值。一个简洁的方法是,例如:

if (!confirm("You have selected A2. Is this correct?")) {
    // event.cancel = true, or whatever you need to do on your side to cancel
} // otherwise fall through and do what you're doing.

就目前而言,由于您从不查看confirm 的返回值,因此您总是会陷入“成功”案例。

【讨论】:

    【解决方案2】:
    var gcse = true, 
        as   = true,
        a2   = true;
    
    if (document.ExamEntry.GCSE.checked == true) {
        gcse = confirm("You have selected GCSE. Is this correct?"));
    }
    
    if (document.ExamEntry.AS.checked == true) {
        as = confirm("You have selected AS. Is this correct?");
    }
    
    if (document.ExamEntry.A2.checked == true) {
        a2 = confirm("You have selected A2. Is this correct?");
    }
    
    if (gcse && as && a2) {
        // you're golden
        window.location.href = 'otherpage'
    }
    

    【讨论】:

      【解决方案3】:
      if (document.ExamEntry.GCSE.checked == true) { 
          if(confirm("You have selected GCSE. Is this correct?")) {
              // do something
          }
      } if (document.ExamEntry.AS.checked == true) {
          if(confirm("You have selected AS. Is this correct?")) {
              // do something
          }
      }  
      if (document.ExamEntry.A2.checked == true) {
          if(confirm("You have selected A2. Is this correct?")) {
              //do something
          }
      }
      

      【讨论】:

      • 谁能美化我的回答!
      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多