[if문]
조건식이 참이면 코드 블록 사이의 실행문을 1회 시행하고 거짓이면 if문을 탈출함
<script>
if(조건식)
{
//조건식이 참이면 실행
}
</script>
[if else문]
조건식이 참이면 실행문 A실행 , 조건식이 거짓이면 실행문B 실행
<script>
if(조건식)
{
실행문 A; //조건식이 참일 경우 실행
}
else
{
실행문 B; //조건식이 거짓일 경우 실행
}
</script>
[ if else if문 ]
두 개 이상의 조건식을 사용하고 싶을 떄 사용할 수 있는 조건문
if, else if, else의 명령어를 모두 사용함
조건식 A가 참이면 실행문 a 가 작동, 조건식 A가 거짓일 경우 조건식 B로 이동
조건식 B가 참이면 실행문 b가 작동되고 조건식 B가 거짓이면 실행문 c가 마지막으로 작동됨
<script>
if(조건식 A)
{
실행문 a; //조건식 A가 참일 떄 실행
}
else if(조건식 B)
{
실행문 b; //조건식 A가 거짓, 조건식 B가 참일 경우 실행
}
else
{
실행문 c; //조건식 A와 B가 모두 거짓일 때 실행
}
</script>
[조건 안에 들어가는 비교 연산자]
전송버튼을 눌렀을때 input에 입력한 값이 공백이면 알림창을 띄워주는 예제
<div class="black-bg">
<div class="white-bg">
<h4>로그인하세요</h4>
<form action="success.html">
<div class="my-3">
<input type="text" class="form-control" id="email">
</div>
<div class="my-3">
<input type="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary" id="send">전송</button>
<button type="button" class="btn btn-danger" id="close" id="close">닫기</button>
</form>
</div>
</div>
1. 먼저 작성할 코드를 한글로 적는다
2. 한글로 작성한 코드를 자바스크립트 언어로 작성해주기
해당 input id를 지정해줌
<script>
$('form').on('submit'), function(){
if(documemt.getElementById('email').value 이 공백이면) {
alert('아이디 입력 안함')
}
})
</script>
'input 이 공백이면'을 자바스크립트로 작성
<script>
$('form').on('submit', function(){
if(document.getElementById('email').value == ''){
alert('아이디입력안함')
}
})
</script>
실행결과
* 알림창도 띄어주고 폼 전송도 막아주는 방법
e. preventDefault()
[ <input>에 입력한 값이 바뀔 때 뭔가 실행하려면? ]
input 이벤트 or change 이벤트 사용
- input 이벤트
Document.getElementBy(‘email’).addEventListener(‘input’, function(){
Console.log(‘안녕’)
}
여기 유저가 입력한 값이 변할 때마다 '안녕'을 출력하게 됨
- change 이벤트
Document.getElementBy(‘email’).addEventListener(‘change’, function(){
Console.log(‘안녕’)
}
change 이벤트는 input에 입력한 값이 바뀌고 포커스를 잃을 때 발생한다는 점에서 input 이벤트와 다름
참고)https://yeolco.tistory.com/98