jquery로 값 확인하기

이소연's avatar
Aug 05, 2024
jquery로 값 확인하기
sample code 만들기
notion image
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> </head> <body> 비번 : <input type="text" id="password" /> 비번체크 : <input type="text" id="passwordCheck" /> <script> $("#passwordCheck").keypress((e)=>{ console.log(e.target.value); }); </script> </body> </html>
notion image
notion image
notion image
notion image
??)target.value 무엇을 확인하는 것?? 저기 어디에 있음??
이벤트가 적용되는 타겟!!
 
일단 값을 비교해야지
내가 하고 싶은 내용
<script> $("#passwordCheck").keypress((e)=>{ console.log(e.target.value); }) $("#password").keypress((e1)=>{ console.log(e1.target.value); }) if(e.target.value == e1.target.value){ console.log("Passwords match"); } else{ console.log("Passwords don't match"); }
 
이 부분을 gpt에 돌려 고쳐달라고 하면
<body> 비번 : <input type="text" id="password"/> 비번체크 : <input type="text" id="passwordCheck"/> <script> function checkPasswords() { const password = $("#password").val(); const passwordCheck = $("#passwordCheck").val(); //사용자가 입력한 password 값을 상수 password에 넣고 //사용자가 입력한 passwordCheck 값을 상수 passwordCheck에 넣고 if (password === passwordCheck) { console.log("Passwords match"); // password랑 passwordcheck랑 맞으면 match } else { console.log("Passwords don't match"); // password랑 passwordcheck랑 안 맞으면 dont't match } } $("#password, #passwordCheck").on('input', () => { checkPasswords(); // input할 때마다 checkpassword 메서드 발동시켜줘 }); </script> </body>
 
??) 근데 const는 불변의 상수인데 비밀번호를 썼다 지웠다 수정 가능한 것??
⇒ let 쓰자요!
 
Share article

Coding's note