简体   繁体   English

如何使用queryselector验证是否填写了两个输入字段?

[英]How to use queryselector to verify if TWO input fields are filled?

I have a function as follows: 我的功能如下:

<script type="text/javascript"> 
function displayquestion(a, ignore) {
    var b = a - 1;
    var currentInput = '';
    var questions = document.getElementsByClassName("questionholder");
    var showRequired = document.getElementById("requiredMessage");

function showNext() {
        showRequired.style.display = "none";

        for (var i = 0; i < questions.length; i++) {
            questions[i].style.display = "none";
        }

        var nextQuestion = document.getElementById("question" + a);

        if (nextQuestion !== null) {
            nextQuestion.style.display = "inline-block";
        }
    }

    // Check if question should ignore inputs
    if (ignore == 1) { // yes, ignore the inputs so move on to next question
        console.log("path 1");

        showNext();
    } else { //no, don't ignore the inputs
        var input = document.querySelector('input.input' + b);
        if (input.type == "radio") { //this is a radio input                
            if (document.querySelector("#question" + b + " input[type=radio]:checked")) { //a radio option is selected
                console.log("path 2");              

                showNext();
            } else { // no radio option is selected so show error                   
                console.log("path 3");

                showRequired.style.display = "block";
            }
        } else { // not a radio input
            if (input !== null) {
                var currentInput = input.value;
            }

            if (currentInput == '') { // the input is blank so show error
                console.log("path 4");

                showRequired.style.display = "block";
            } else { // the input is not blank so move on to next question
                console.log("path 5");

                showNext();
            }
        }
    }
}
</script>
<div id="requiredMessage" style="display:none"><p>This field is required.</p></div>

<form id="TheForm" style="display:block;">
    <div class="questionholder" id="question1" style="display:inline-block"> <!-- REQUIRED -->
        <div style="display:inline-block">
            <h5>Given Name</h5>
            <input class="input1" name="gn"><br>    
        </div>
        <div style="display:inline-block">
            <h5>Last name</h5>
            <input class="input1" name="ln"><br>    
        </div>
        <div class="holdButtons">
            <a class="text2button" onclick="displayquestion(2)">Next</a>
        </div>
</div>
<div class="questionholder" id="question2" style="display:none"> <!-- REQUIRED -->
it worked!
</div>
</form>

Where I want to bring particular focus on this segment: 我要特别关注这一部分的地方:

    var input = document.querySelector('input.input' + b);
} else { // not a radio input
        if (input !== null) {
            var currentInput = input.value;
        }

        if (currentInput == '') { // the input is blank so show error
            console.log("path 4");

            showRequired.style.display = "block";
        } else { // the input is not blank so move on to next question
            console.log("path 5");

            showNext();
        }
    }

The gist of the code is that the user should be required to fill in BOTH inputs fields. 该代码的要旨是应要求用户填写两个输入字段。 If EITHER input field is blank, the error message should appear. 如果EITHER输入字段为空,则将出现错误消息。

However, my code only shows the error in cases where BOTh input fields are blank or if only the first input field is blank. 但是,我的代码仅在BOTH输入字段为空白或仅第一个输入字段为空白的情况下显示错误。

If the first input field filled in but the second is blank, the user should be shown the error message - but this isn't happening. 如果第一个输入字段已填写,但第二个字段为空白,则应向用户显示错误消息-但这没有发生。

From what i understand, my code isn't actually checking both input fields, but instead is only checking if either input is blank but I'm not clear on how I can correct my code to iterate over both input fields to check that they are both filled in. 据我了解,我的代码实际上并没有检查两个输入字段,而是仅检查两个输入字段是否为空,但是我不清楚如何纠正我的代码以遍历两个输入字段来检查它们是否为空。都填写。

No jQuery please. 请不要jQuery。 Thank you. 谢谢。

https://jsfiddle.net/vj9mk1bq/1/ https://jsfiddle.net/vj9mk1bq/1/

querySelector() only finds first matching element querySelector()仅找到第一个匹配元素

For multiple inputs you could do something like 对于多个输入,您可以执行类似的操作

var inputsArray = Array.from(document.querySelectorAll(selector));

var isValid = inputsArray.every(function(el){
   return el.value; // empty string is falsy
});

Basically using Array#every() to return a boolean based on each element having a value. 基本上使用Array#every()基于每个具有值的元素返回一个布尔值。 You can adjust the return for more granular validation as needed 您可以根据需要调整return以进行更精细的验证

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

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