我有 4 個復選框,我想在選擇前兩個復選框時顯示不同的警報,但沒有得到輸出。請告訴我哪里出錯了。
let ReviewCheckBox = checkform.ratings;
if (ReviewCheckBox[0].checked == true) {
alert("1 Selected");
} else if (ReviewCheckBox[0].checked == true && ReviewCheckBox[1].checked == true) {
alert("Both Selected");
}
<div>
<span>CUSTOMER REVIEWS</span><br>
<form name="checkform" onclick="productFilter()">
<input type="checkbox" name="ratings" id="rating-checka">
<label for="rating-checka">4 * & Above</label><br>
<input type="checkbox" name="ratings" id="rating-checkb">
<label for="rating-checkb">3 * & Above</label><br>
<input type="checkbox" name="ratings" id="rating-checkc">
<label for="rating-checkc">2 * & Above</label><br>
<input type="checkbox" name="ratings" id="rating-checkd">
<label for="rating-checkd">1 * & Above</label>
</form>
</div>
uj5u.com熱心網友回復:
1)您必須對選中的每個復選框進行驗證,以便為此您可以創建一個函式,productFilter并且每次選中復選框時它都會運行
2)您必須先檢查0和1索引復選框only 0。
function productFilter() {
let ReviewCheckBox = checkform.ratings;
if (ReviewCheckBox[0].checked == true && ReviewCheckBox[1].checked == true) {
alert("Both Selected");
} else
if (ReviewCheckBox[0].checked == true) {
alert("1 Selected");
}
}
<div>
<span>CUSTOMER REVIEWS</span><br>
<form name="checkform" onclick="productFilter()">
<input type="checkbox" name="ratings" id="rating-checka">
<label for="rating-checka">4 * & Above</label><br>
<input type="checkbox" name="ratings" id="rating-checkb">
<label for="rating-checkb">3 * & Above</label><br>
<input type="checkbox" name="ratings" id="rating-checkc">
<label for="rating-checkc">2 * & Above</label><br>
<input type="checkbox" name="ratings" id="rating-checkd">
<label for="rating-checkd">1 * & Above</label>
</form>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367433.html
標籤:javascript 验证 复选框
