除了确保他们参加实验室课程外,我还试图评估一个人是否满足完成数学,科学或化学课程的要求。
$(document).ready(function() {
$("#thebtn").click(function() {
if ($(chemlab).is(':checked') || ($(physlabone).is(':checked') || ($(physlabtwo).is(':checked') || ($(physlabthree).is(':checked'))))) {
var labreq = true;
}
if (($('.mathh:checkbox:checked') && $('.chemm:checkbox:checked')) || ($('.mathh:checkbox:checked') && $('.physs:checkbox:checked')) || ($('.physs:checkbox:checked') && $('.chemm:checkbox:checked'))) {
var dispreq = true;
}
if (dispreq == true && labreq == true) {
$("#Feedback_Science_Complete").html("You've made it!");
}
});
});
当我运行以下代码时,它将返回“您成功做到了!”如果选中仅实验室课程,则当目标是他们需要实验室以及其他学科的课程时。我以为我可以使用if语句来搜索是否选中了复选框类,然后仅使用&&运算符将其与2个不同的类进行比较。出于某种原因,该代码仅在满足1个实验室条件时才能工作。
<p><input type="checkbox" id="chemlab" class="chemm"> CHEM& 161 General Chemistry w/ Lab 1</p>
<p><input type="checkbox" id="stats" class="mathh"> MATH 146 Introduction to Statistics OR</p>
<p><input type="checkbox" id="calc" class="mathh"> MATH& 163 Calculus 3 </p>
<p><input type="checkbox" id="physlabone" class="physs"> PHYS& 114 General Physics with Lab 1</p>
<p><input type="checkbox" id="physlabtwo" class="physs"> PHYS& 115 General Physics with Lab 2</p>
<p><input type="checkbox" id="physlabthree" class="physs"> PHYS& 116 General Physics with Lab 3</p>
<p id="Feedback_Science_Complete" class="positive"></p>
<p> <input type="button" id="thebtn" value="Click here to see if you meet the reqs" class='btn'></p>
上面是复选框和按钮的代码。或下面的内容足以重现该问题。
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody><tr>
<td>
<h4>Quantitative or Symbolic Reasoning: </h4>
<p>Complete all of the following</p>
<p id="Feedback_QSR_Complete" class="positive"></p>
<p id="Feedback_QSR_Incomplete" class="negative"></p>
<p id="Feedback_Missing_Calc_1" class="negative"></p>
<p id="Feedback_Missing_Calc_2" class="negative"></p>
<p><input type="checkbox" checked="checked" id="mone"> MATH& 151 Calculus I</p>
<p><input type="checkbox" id="mtwo"> MATH& 152 Calculus II</p>
<h4>Foundations for College Success</h4>
<p id="Feedback_ColSucc_Complete" class="positive"></p>
<p id="Feedback_ColSucc_Incomplete" class="negative"></p>
<p>Complete at least one of following</p>
<p><input type="checkbox" id="cone"> COLL 100 Study Strategies</p>
<p><input type="checkbox" checked="checked" id="ctwo"> COLL 101 College Strategies</p>
</td><td>
<h4>Science Distribution Requirement</h4>
<p>Students must complete courses from at least two different disciplines,
and include at least one lab course </p>
<p id="Feedback_Science_Complete" class="positive"></p>
<p id="Feedback_Science_Incomplete" class="negative"></p>
<p></p>
<p id="Feedback_Science_No_Lab" class="negative"></p>
<p id="Feedback_Science_Missing_Second_Discipline" class="negative"></p>
<p><input type="checkbox" id="chemlab" class="chemm"> CHEM& 161 General Chemistry w/ Lab 1</p>
<p><input type="checkbox" id="stats" class="mathh"> MATH 146 Introduction to Statistics OR</p>
<p><input type="checkbox" id="calc" class="mathh"> MATH& 163 Calculus 3 </p>
<p><input type="checkbox" id="physlabone" class="physs"> PHYS& 114 General Physics with Lab 1</p>
<p><input type="checkbox" id="physlabtwo" class="physs"> PHYS& 115 General Physics with Lab 2</p>
<p><input type="checkbox" id="physlabthree" class="physs"> PHYS& 116 General Physics with Lab 3</p>
</td></tr></tbody></table>
<script>
$(document).ready(function() {
$("#thebtn").click(function() {
if ($(mone).is(':checked') && ($(mtwo).is(':checked'))) {
$("#Feedback_QSR_Complete").html("Cogratulations! You've met the Quantitative and Symbolic Reason requirements!");
} else if
(!$(mone).is(':checked') && ($(mtwo).is(':checked'))) {
$("#Feedback_Missing_Calc_1").html("You are missing Calculus 1!");
} else if
($(mone).is(':checked') && (!$(mtwo).is(':checked'))) {
$("#Feedback_Missing_Calc_2").html("You are missing Calculus 2!");
} else if
(!$(mone).is(':checked') && (!$(mtwo).is(':checked'))) {
$("#Feedback_QSR_Incomplete").html("You haven't completed any courses here!");
}
if ($(cone).is(':checked') || ($(ctwo).is(':checked'))) {
$("#Feedback_ColSucc_Complete").html("You've satisfied the Foundations for College Sucess requirements!");
} else if
(!$(cone).is(':checked') && (!$(ctwo).is(':checked'))) {
$("#Feedback_ColSucc_Incomplete").html("You've not completed any courses here!");
}
if ($('#chemlab').is(':checked') || ($('#physlabone').is(':checked') || ($('#physlabtwo').is(':checked') || ($('#physlabthree').is(':checked'))))) {
var labreq = true;
}
if (($('.mathh:checkbox:checked') && $('.chemm:checkbox:checked')) || ($('.mathh:checkbox:checked') && $('.physs:checkbox:checked')) || ($('.physs:checkbox:checked') && $('.chemm:checkbox:checked'))) {
var dispreq = true;
}
if (dispreq == true && labreq == true) {
$("#Feedback_Science_Complete").html("You've made it!");
}
});
});
</script>
<p> <input type="button" id="thebtn" value="Click here to see if you meet the reqs" class='btn'></p>
</head>
</html>