js两个if嵌套在一起是如何运行的?

如这个登录验证的例子,两个if嵌套在一起是怎么运行的?

<!DOCTYPE html>
<html>

<head>
    <title>login</title>
    <meta charset="utf-8">
</head>

<body>
    <form id="loginform" name='loginform' onsubmit="return checkLogin()">
        <div>
            <label for="username">账号:</label>
            <input type="text" id="username" name="username">
        </div>
        <div>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password">
        </div>
        <input type="submit" value="登录">
    </form>
    <script type="text/javascript">
    //验证登录
    function checkLogin() {
        if (loginform.username.value != "") {
            if (loginform.password.value != "") {
                return ture
            } else {
                alert('密码不能为空');
                return false
            }

        } else {
            alert('用户名不能为空');
            return false
        }
    }
    </script>
</body>

</html>
阅读 6.3k
2 个回答
st=>start: Start
cond=>condition: 用户名是否不为空?
cond2=>condition: 密码是否不为空?
e=>end: alert('用户名不能为空')
e2=>end: 通过
e3=>end: alert('密码不能为空')
st->cond
cond(no)->e
cond(yes)->cond2(yes)->e2
cond(yes)->cond2(no)->e3
var username = loginform.username.value != "";
var password = loginform.password.value != "";
if(!username ){
    alert('用户名不能为空');
}
if(!password){
    alert('密码不能为空');
}
return username && password;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题