【问题标题】:Trapping a Cancel in a JavaScript Confirm Prompt?在 JavaScript 确认提示中捕获取消?
【发布时间】:2021-02-28 19:18:18
【问题描述】:

今天早上有些东西出了问题,我很确定是我的大脑造成的。我有一个表单可以在特定条件下跟踪字段的更改:

-如果状态 = 草稿,则什么也不做。 - 如果状态 = 已批准,提示用户询问他们是否确定要进行更改。 - 如果他们单击“确定”,它应该调用记录更改的进程。 - 如果他们点击“取消”,它应该什么都不做。 - 如果状态不是草稿或已批准,不要提示,而是调用记录更改的进程。

我有记录非草稿状态更改的代码,但是在我将 ==true 添加到确认语句的末尾后,一切都停止了。

var stat = $('#status').text(); 
var parms = just a bunch of parameters i'm passing to the url below;

if (stat=='(Draft)'){
//do nothing
}
else if (stat !== 'Approved' && stat!== '(Draft)'){
var url = webDbName + '/(CreateOTChangeRecordOnChange)?OpenAgent' + parms;
        $.get(url, function(data) {
            $('#tableBodyChanges').html(data);
});
}

}
else if (stat == 'Approved' && confirm('You are changing the hours on a request that has already been approved. This will send a notification to the department director. Proceed?')==true) {
  var url = webDbName + '/(CreateOTChangeRecordOnChange)?OpenAgent' + parms;
        $.get(url, function(data) {
            $('#tableBodyChanges').html(data);
                                });
} else {
//do nothing                                });
}

【问题讨论】:

  • 你在最后一个 else if 块之前有一个额外的 }

标签: javascript confirm


【解决方案1】:

它正在工作,但你可以省略 == true,因为它已经是一个“if-able boolean”

if(confirm("test?")){
   console.log("confirmed1");
}else {
   console.log("notconfirmed1");
}

<!-- or the other way arround -->

if(!confirm("test?")){
   console.log("notconfirmed2");
}else {
   console.log("confirmed2");
}

<!-- it should also work the way you posted as well -->

if("foo" == "foo" && confirm("test?") == true){
   console.log("confirmed3");
}else {
   console.log("notconfirmed3");
}

【讨论】:

  • 同意 - 它应该像我得到的那样工作,但没有。 :(
  • 比我说的变量stat 有你不期望的内容,你也应该调试它并查看它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多