我试图仅在 if 语句返回 true 时调用该函数。下面的函数检查用户名字段以确保其中有内容(如果有)将其发送到函数验证表单
function usernamecheck() {
if ($("#signupUsername").val().length < 4) {
return true;
}
}
function validateForm() {
if (usernamecheck(returns true)) {
//run code
}
}
这可能/最好的方法
最佳答案
function usernamecheck() {
//Updated this to just return the expression. It will return true or false.
return $("#signupUsername").val().length < 4;
}
function validateForm() {
//Here we just call the above function that will either return true or false.
//So by nature the if only executes if usernamecheck() returns true.
if (usernamecheck()) {
//Success..Username passed.
}
}
关于javascript - 仅当返回 true 时才可以在 if 语句中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170506/