我可以在这里看到警报消息。即使返回 false,我也可以看到我的表单发布它。代码哪里坏了?
function ValidateForm(){
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];
formelements.forEach(function(obj) {
if(obj.value=="") {
obj.style.borderColor = "#FF0000";
alert(obj);
return false;
}
});
我的 HTML 代码
<form action="product_formhandler.php" name="addProduct" id="addProduct" onsubmit="return ValidateForm();" method="post">
<table>
<tr>
<td>
Product Name:
</td>
<td>
<input type="text" id="product_name" name="product_name" />
</td>
</tr>
<tr>
<td>
Part number:
</td>
<td>
<input type="text" id="part_number" name="part_number" />
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
<textarea id="description" name="description" rows="8" col="25"></textarea>
</td>
</tr>
<tr>
<td>
Price:
</td>
<td>
<input type="text" id="price" name="price" />
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="reset" id="reset" value="Reset" />
<input type= "hidden" name="addproduct" value="1" />
<input type="submit" id="addProductSubmit" name="action" value="Add" />
</td>
</tr>
</table>
</form>