简体   繁体   English

JavaScript:关闭正确值的模态

[英]JavaScript: Close Modal on correct Values

Good Day,再会,

I am currently nearly done creating a Modal with Vanilla JS, at present, my "Age Gate" does check if the age of the user is less than 18 and displays a error of 'Invalid Credentials',我目前几乎完成了使用 Vanilla JS 创建模式,目前,我的“年龄门”会检查用户的年龄是否小于 18 岁并显示“无效凭据”错误,

I have tried to go further by trying to close the modal on the correct value and then the modal to close,我试图通过尝试关闭正确值的模态然后关闭模态来进一步尝试,

Please see my code below for the JS:请在下面查看我的 JS 代码:

const modal = document.getElementById("myModal");
const form = document.querySelector('form');
const loading = document.getElementById("loading");
const btnSubmit = document.getElementById('btnSubmit');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
};

// Functions

function onlyNumbers(e) {
  const ageGate = document.querySelectorAll('.ageGate');
  const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  const errMsgDiv = document.getElementById('errorMsg');

  let containsNumber = false
  ageGate.forEach(input => {
    if(numbers.some(num => input.value.includes(num))) containsNumber = true;
  })
  if (containsNumber || ageGate.value !== numbers && ageGate.value == '' || ageGate[2].value < 2004) {
    let paragraph = document.createElement('p');
    paragraph.innerHTML = 'Invalid Credentials';
    errMsgDiv.append(paragraph);
    e.preventDefault()
    }  else if (containsNumber = true) {
        btnSubmit.remove()
        loading.classList.remove('hide')
    }
    
};

and now the Modal html:现在是模态 html:

<div id="modal">
            <div class="modal-content">
                <div class="flex-container">
                    <div>
                        <img
                            src="{% static 'imgs/Fireball_logo.png' %}"
                            class="logo"
                            alt="fireball logo"
                        />
                        <h1>WOAH THERE!</h1>
                        <p class="info-text">We just need to see something</p>
                    </div>
                    <form action="">
                        {% csrf_token %}
                        <div class="form-flex">
                            <input
                                class="ageGate"
                                type="text"
                                max-length="2"
                                placeholder="DD"
                            />
                            <input
                                class="ageGate"
                                type="text"
                                max-length="2"
                                placeholder="MM"
                            />
                            <input
                                class="ageGate"
                                type="text"
                                max-length="4"
                                placeholder="YYYY"
                            />
                        </div>
                        <p class="cookie-policy">
                            <small>
                                This site uses cookies. Cookie Policy. I agree
                                to the terms of use, and the privacy policy.
                                This information will not be used for marketing
                                purposes
                            </small>
                        </p>
                        <div class="text-center">
                            <button id="btnSubmit" class="btn" type="submit">
                                <p class="btn-text">Enter</p>
                            </button>
                            <img 
                                src="{% static 'imgs/spinner.gif' %}"
                                alt="spinner" 
                                id="loading" 
                                class="loading close">
                        </div>
                        <span id="errorMsg"></span>
                    </form>
                    <div>
                        <img
                            src="{% static 'imgs/Drinkaware_logo.png' %}"
                            alt="Drinkaware Logo"
                        />
                    </div>
                </div>
            </div>
        </div>

Please as usual, I am sort of new with it all and I am definitely trying, so in your help, please could I ask for a brief explanation on your solution so that I may see how to think as a dev as that kind of information does help me请像往常一样,我对这一切有点陌生,我肯定会尝试,所以在你的帮助下,我能否要求对你的解决方案做一个简短的解释,以便我可以看到如何作为开发人员思考这类信息对我有帮助

Thanks!谢谢!

function onlyNumbers(e) {
  e.preventDefault()
  const ageGate = document.querySelectorAll('.ageGate');
  const errMsgDiv = document.getElementById('errorMsg');

  const year = ageGate[2].value;
  const month = ageGate[1].value;
  const day = ageGate[0].value;
  const userBirthday = new Date(year, month - 1, day);

  // Date Right Now
  const today = new Date();
  // Users age in years
  const acceptingYear = today.getFullYear() - userBirthday.getFullYear()

  if (acceptingYear < 18 || 
      acceptingYear == 18 &&
      today.getMonth() < userBirthday.getMonth() ||
      today.getMonth() == userBirthday.getMonth() &&
      today.getDate() < userBirthday.getDate()
      ) {
      let paragraph = document.createElement('p');
      paragraph.innerHTML = 'Invalid Credentials';
      errMsgDiv.append(paragraph);
  } else {
    btnSubmit.remove()
    loading.classList.remove('hide')
    modal.classList.add('close');
    }
    
};

I needed to target the modal and make sure that it was closing on execution of my function我需要定位模式并确保它在执行我的功能时关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM