mercredi 24 mars 2021

Doesn't JavaScript button onClick event when you get a global variable?

I made a simple click event. But if I use js dom to make it a global variable, it doesn't seem to work. Is there a genius here who can explain the reason easily to me?

This is html file very very simple!

 <form>
      <input type="radio" name="a" id="ck1" /> 수신동의
      <input type="radio" name="a" id="ck2" /> 수신차단
      <input type="button" value="서비스 등록" onclick="checkHandle()" />
 </form>

This is JS file.

let ck1 = document.getElementById("ck1").checked;
let ck2 = document.getElementById("ck2").checked;
function checkHandle() {
  console.log(ck1);
  if (ck1 === false && ck2 === false) {
    alert("광고 수신 동의에 대해 선택해주세요.");
  } else {
    alert("서비스를 등록합니다.");
  }
}

or

function checkHandle() {
let ck1 = document.getElementById("ck1").checked;
let ck2 = document.getElementById("ck2").checked;
  console.log(ck1);
  if (ck1 === false && ck2 === false) {
    alert("광고 수신 동의에 대해 선택해주세요.");
  } else {
    alert("서비스를 등록합니다.");
  }
}

To be exact again what I want to say, I would like to ask you why declaring the ck1, ck2 variable declaration within the checkHandle function is different from declaring it as a global variable. Please explain it easily.

For your information, it didn't work properly when I declared it a global variable.




Aucun commentaire:

Enregistrer un commentaire