Front-end/jQuery

[jQuery 시작하기] 01. HTML, CSS와의 콜라보레이션

Dev다D 2021. 2. 16. 22:25
반응형

아이디 비밀번호 검사

 

자바스크립트를 활용해서 '로그인 페이지'를 수정하려고 하는데요.

  1. 아이디 혹은 비밀번호 둘 중 하나라도 입력란이 비어있다면, 로그인 버튼이 '비활성화'되어서 버튼의 배경색이 #9B9B9B로 설정되어야 합니다.
  2. 사용자가 아이디와 비밀번호를 둘 다 입력하면, 로그인 버튼이 '활성화'되어서 버튼의 배경색이 #1BBC9B로 바뀌어야 합니다.

아이디와 비밀번호 모두 입력할 경우에만 로그인 버튼 활성화
<!DOCTYPE html>
<html>
<head>
    <title>로그인</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="styles.css">
    <link href="https://fonts.googleapis.com/earlyaccess/notosanskr.css" rel="stylesheet">
</head>
<body>
<div class="login-form">
    <form>
        <input type="text" name="email" id="email-input" class="text-field" placeholder="아이디"><br>
        <input type="password" name="password" id="password-input" class="text-field" placeholder="비밀번호"><br>
        <input type="submit" value="로그인" id="submit-btn">
    </form>

    <div class="links">
        <a href="#">비밀번호를 잊어버리셨나요?</a>
    </div>
</div>

<script
        src="https://code.jquery.com/jquery-3.2.1.min.js"
        integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
        crossorigin="anonymous"></script>

<script>
    $('#email-input').on('input', checkInput);
    $('#password-input').on('input', checkInput);

    function checkInput() {
        var email = $('#email-input').val();
        var password = $('#password-input').val();

        if (email === '' || password === '') {
            //비활성화
            $('#submit-btn').css('background-color', '#9B9B9B');
        } else {
            //활성
            $('#submit-btn').css('background-color', '#1BBC9B');
        }
    }


</script>
</body>
</html>
* {
    box-sizing: border-box;
    font-family: 'Noto Sans KR', sans-serif;
}

body {
    margin: 0;
    background-color: #1BBC9B;
}

.login-form {
    width: 300px;
    background-color: #eeeff1;
    margin-left: auto;
    margin-right: auto;
    margin-top: 50px;
    border-radius: 5px;
    padding: 20px;
}

.text-field {
    font-size: 14px;
    width: 100%;
    border: none;
    border-radius: 5px;
    padding: 10px;
    margin-bottom: 10px;
}

#submit-btn {
    font-size: 14px;
    background-color: #9B9B9B;
    color: white;
    width: 100%;
    border-radius: 5px;
    padding: 10px;
    border: none;
}

#submit-btn:hover {
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.50);
}

.links {
    margin-top: 30px;
    text-align: center;
}

.links a {
    font-size: 12px;
    color: #9b9b9b;
}

본 내용은 Codeit의  'jQuery' 강의를
참고하여 작성한 내용입니다.
반응형