[英]How to use multiple Javascript statement conditions with an “OR” confirm
I have the following function containing a condition in javascript. 我有以下函数,其中包含javascript中的条件。
function dosomething(id, action) {
if (action != 'delete' || confirm("Are you sure?")) {
alert('if action equals delete, then it has been confirmed');
// long lines of code executed here.
}
}
In the above function condtion, it checks if action is NOT equal to "delete". 在上述功能条件中,它检查动作是否不等于“删除”。 If its equal delete, it gives a confirmation before proceeding. 如果相等,则在继续操作之前会给出确认。 But I'd like to add another value as well as "delete", where the "Are you sure?" 但是我想在“您确定吗?”的位置添加另一个值以及“删除”。 message shows/needs confirmation. 消息显示/需要确认。
I am unable to do this and I've tried the following. 我无法执行此操作,并且尝试了以下操作。
if ((action != 'delete' || action != 'report') || confirm("Are you sure?")) { ...
What I am trying to do is, if the action == delete or report, the confirmation message should pop-up. 我想做的是,如果操作==删除或报告,则将弹出确认消息。
I don't want to write two different if statements (as shown below), as there is a lot of code that needs to execute after confirmation, and it will be bad practice. 我不想编写两个不同的if语句(如下所示),因为确认后需要执行很多代码,这将是一种不好的做法。
if (action != 'delete' || confirm("Are you sure?")) {
THEN 然后
if (action != 'report' || confirm("Are you sure?")) {
THANKS 谢谢
It would probably be clearer to do something like this, reducing indentation and the need for careful reading of the boolean logic: 做这样的事情,减少缩进和仔细阅读布尔逻辑的需求可能会更清楚:
function dosomething(id, action) {
if (action === 'delete' || action === 'report') {
if (!confirm("Are you sure?")) return;
}
// long lines of code executed here.
}
It would be possible to write it all in one check like 有可能像一张一张一样写所有
if ((action === 'delete' || action === 'report') && !confirm("Are you sure?")) return;
but that's less readable IMO. 但这是不太容易理解的IMO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.