我有一個帶有4個複選框的窗體,並且如果未選中框,我必須在提交窗體時顯示錯誤消息。這是我迄今爲止所做的,我只是不知道自己出錯的地方。如何驗證一組複選框並返回錯誤信息javascript
Im new to javascript。我已經驗證正在加載身體標籤,我剛剛包括必要的部分。預先感謝您的幫助。
function validate() {
var choice1 = document.getElementById("base_small");
var choice2 = document.getElementById("base_medium");
var choice3 = document.getElementById("base_large");
var choice4 = document.getElementById("base_xlarge");
if ((choice1 == "") && (choice2 == "") && (choice3 == "") && (choice4 == "")) {
alert("Please choose a base")
return false;
}
return true;
}
<div class="form1">
<h3>Please select a base size**:</h3>
<form id="orderformbase" class="orderbase" onsubmit="return validate()">
<label class="block"><input type="radio" name="base" value="base_small" id="base_small">Small<span class="price"> £5</span></label>
<label class="block"><input type="radio" name="base" value="base_medium" id="base_medium">Medium<span class="price"> £7.50</span></label>
<label class="block"><input type="radio" name="base" value="base_large" id="base_large">Large<span class="price"> £10</span></label>
<label class="block"><input type="radio" name="base" value="base_xlarge" id="base_xlarge">X-Large<span class="price"> £12.50</span></label>
</div>
<!-- form1 close -->
</form>
您檢索checkboxe輸入,但沒有核實其'checked' propertie(布爾)。 – Booster2ooo