javascript - 如何以正确的形式编写 if else 条件
问题描述
如何写简单if
的取第一个表达式。下面的例子,我的代码看起来很像。我的意思是第一个条件email.val()
和第二个 条件!validateEmail(email.val())
或表达式。我的大问题是如何检测执行第一个或第二个条件?
if(email.val() == "" || !validateEmail(email.val())){
//call the condition again
if(email.val() ==""){
$("#error").html("<p>Email Cant be empty</p>");
$("#error").show();
setTimeout(function(){$("#error").fadeOut();}, 2000)
}else{
$("#error").html("<p>Wrong email format</p>");
$("#error").show();
setTimeout(function(){$("#error").fadeOut();}, 2000)
}
email.focus();
}
所以我不需要if
再次调用它
if(email.val() == ""){
$("#error").html("<p>Email Cant be empty</p>");
$("#error").show();
setTimeout(function(){$("#error").fadeOut();}, 2000)
}else{
$("#error").html("<p>Wrong email format</p>");
$("#error").show();
setTimeout(function(){$("#error").fadeOut();}, 2000)
}
解决方案
你大概有
if (condition1 || condition2) {
if (condition1) {
foo();
} else {
bar();
}
moo();
}
else
只能在 和 时触发,condition1==false
因此condition2==true
您可以编写与
if (condition1) {
foo();
moo();
} else if (condition2) {
bar();
moo();
}
推荐阅读
- android - 对于某些特定视频的音频,我使用 mediaExtractor 和 mediaFormat 获得一半的通道数和一半的采样率
- node.js - TypeError: expect(...).toBe 不是一个函数
- uml - 时序图中的同步消息是否必须要有回复消息
- r - 每个流体行的标题
- ampl - AMPL需要下标和语法错误问题
- python - 如何使用表单更新 Django 模型中的记录?
- java - ImageIcon 没有出现在 JLabel 或 JButton 上
- sql - 具有多个嵌套条件的 SQL 选择查询问题
- asp.net-core - ASP.Net Core 应用程序 - 设置内存限制
- r - 如何在控制台上创建没有返回值的中断函数?