我能夠在此處看到警報消息。即使返回false後,我也可以看到我的表單發佈。代碼在哪裏被破壞?javascript不返回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>
您是否收到任何錯誤? –
nope。它只是提醒我的消息,並繼續張貼表格。 – JakeNsmith