首页 > 解决方案 > 代码执行不起作用,因为代码不会导致框显示为红色

问题描述

执行方式 不确定是否在执行Java脚本

        form.addEventListener('Submit', (e) => {
            e.preventDefault():
            checkInputs();
        
        });

用于检查来自 html 文件的输入的函数

        function checkInputs() {
            // get the values form the inputs
            const firstnameValue = first.value.trim();
            const lastnameValue = second.value.trim();
            const numberValue = phone.value.trim();
            const amountValue = amount.value.trim();
    

必须满足的条件

            if(firstnameValue === '') {
                // show error
                // add error class
                setErrorFor(Forename, "Username is invlaid");
            } else {
                // add success class
                setSucessFor(Forename);
            }
        }

不确定为什么该功能不起作用

        function setErrorFor(input, message) {
            const formControl = input.parentElement; // .form-control
            const small = formControl.querySelector('small');
        
            // add error message inside small
            small.innerText = message;
        
            // add errror class
            formControl.className = 'form-control error';
        }

标签: javascripthtmlcss

解决方案


推荐阅读