【发布时间】:2021-10-14 18:54:21
【问题描述】:
我正在尝试创建一个基于必填字段进行验证的多步骤表单。 我正在使用的当前 Javascript 只是查找填写的字段以验证表单。但是如何让它忽略 html 中未标记为“必填”的字段?
谢谢!
function validateForm() {
// This function deals with validation of the form fields
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
// and set the current valid status to false:
valid = false;
}
}
// If the valid status is true, mark the step as finished and valid:
if (valid) {
document.getElementsByClassName("step")[currentTab].className += " finish";
}
return valid; // return the valid status
}
【问题讨论】:
-
你会用那些单字母的变量名树敌。只是不要。
标签: javascript forms