用戶輸入輸入,單擊一個按鈕,然後調用一個函數來執行一些計算。如果某個字段的類型或值不正確,則會顯示錯誤消息。如果其中一個字段錯誤,那麼該字段需要顯示錯誤消息。如果多個字段錯誤,那麼這些字段需要顯示錯誤消息。現在,即使所有字段都是錯誤的,如果字段錯誤,頁面也只顯示一條錯誤消息。我確信這是一個簡單的解決方法,但我一直在撞牆。有什麼建議麼?謝謝!這裏是代碼適用部分:JavaScript多個錯誤消息
if (isNaN(theMilesDriven) || theMilesDriven <= 0) {
theMilesDrivenError.firstChild.nodeValue = "Please enter a number greater than 0 for the miles driven.";
return false;
}
if (isNaN(theGallonsUsed) || theGallonsUsed <= 0) {
theGallonsUsedError.firstChild.nodeValue = "Please enter a number greater than 0 for the gallons of gas used.";
return false;
}
if (isNaN(thePriceGallon) || thePriceGallon <= 0) {
thePriceGallonError.firstChild.nodeValue = "Please enter a number greater than 0 for the price per gallon.";
return false;
}
'return'語句立即退出函數。 – Pointy
刪除'return'語句 –
因此,如果我完全刪除了返回語句並將計算放在else語句中,那麼這看起來像是一個有效的修復嗎?謝謝! – MatthewSpire