0

我有 4 个表单输入,product_name、part_number、description、price。我有 onsubmit 函数 validateForm1(); 最初在提交具有空值的每个输入字段时,所有应用的错误类。现在我的问题是当我提交时它不会返回原始类。

如果前两个输入字段不为空,则在提交第二个输入字段时不会第一次返回原始类。第二次,它返回。第 3 次输入需要 5 次提交才能返回原始课程。

请把我看作 javascript 和 html 的新手

function ValidateForm1(){
var productName=document.addProduct.product_name;
var partNumber=document.addProduct.part_number;
var description=document.addProduct.description;
var price=document.addProduct.price;

var formelements =[productName, partNumber, description, price];

var forEachResult = true;
formelements.forEach(function(obj) {
if(obj.value=="") {
obj.className = obj.className + " error";  // this adds the error class

//alert(obj);
/* Rather than returning false, set the variable to false instead. */
forEachResult = false;
/* Break the loop. */
return;
} else {
    obj.className =  obj.className.replace(" error", ""); // this removes the error class
    return;
}
/* Return the variable which will either be true or false. */

})
return forEachResult; 
}
4

0 回答 0