javascript - 仅当返回 true 时才可以在 if 语句中调用函数

标签 javascript

我试图仅在 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/

相关文章:

javascript - 如果交易 ID 更改,如何识别格子交易

javascript - 使用 Apps Script API 的 Chrome 应用

php - 在 Safari 中使用 javascript history.back() 失败 .. 我如何让它跨浏览器?

javascript - 将 Enter 键映射到 HTML/CSS/JS 中的按钮

javascript - CSS 与 JavaScript 水平滑动/过渡

javascript - IIS URL 重写 CSS url() 值的问题

javascript - 如何在ajax回调中从 'success'调用代码隐藏方法?

javascript - 鼠标滚轮滚动事件

javascript - Jest Test 检查 redux-form 名称

Javascript 在我锁定 iPhone 时停止,它还能运行吗?