我有这个功能:
function RemoveProduct() {
if (confirm("Poista?") == return true) {
return true;
} else {
return false;
}
}
当您点击页面上的“删除”按钮时,它会询问是否应该删除产品,如果答案是肯定的,它就会删除它。
但据我所知,我不能在if语句条件上使用另一个括号? 应该怎么做?
最佳答案
当您将返回值与 true
进行比较时,您不应使用 return true
,而应使用 true
:
function RemoveProduct() {
if (confirm("Poista?") == true) {
return true;
} else {
return false;
}
}
你甚至不需要做比较,因为 confirm
的结果是一个 bool 值:
function RemoveProduct() {
if (confirm("Poista?")) {
return true;
} else {
return false;
}
}
而且你甚至不需要if
语句,你可以直接从confirm
返回结果:
function RemoveProduct() {
return confirm("Poista?");
}
在事件中使用该函数时,请记住使用return
。示例:
<input type="submit" onclick="return RemoveProduct();" />
关于javascript - 使用 confirm() 作为 if 的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12073352/