gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:13:25 24 4
gpt4 key购买 nike

我有一个功能如下:

<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>

我想特别关注这个分割市场:

    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();
}
}

代码的要点是应该要求用户填写两个输入字段。如果任一输入字段为空,则应显示错误消息。

但是,我的代码仅在两个输入字段都为空或只有第一个输入字段为空的情况下才会显示错误。

如果第一个输入字段已填写,但第二个输入字段为空,则应向用户显示错误消息 - 但这种情况不会发生。

据我了解,我的代码实际上并没有检查两个输入字段,而只是检查两个输入字段是否为空,但我不清楚如何更正我的代码以迭代两个输入字段以进行检查他们都被填满了。

请不要使用 jQuery。谢谢。

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

最佳答案

querySelector() 仅查找第一个匹配元素

对于多个输入,您可以执行类似的操作

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

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

基本上使用Array#every()根据每个具有值的元素返回一个 bool 值。您可以根据需要调整返回以进行更精细的验证

关于javascript - 如何使用 queryselector 验证两个输入字段是否已填写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54620680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com