javascript - 在显示输出之前先比较所有 If 语句
问题描述
我以为我已经弄清楚了逻辑;但是,我得到的输出是第一个被发现为真的 IF 语句。为了在显示警告框之前先通过逻辑并找到正确的输出,我是否需要使用 switch 语句?
如果是这样,这看起来像什么?我一直在尽职调查,似乎无法弄清楚 switch 语句如何适用于我的情况。
function get_recommendation() {
var ES = "Everyday Solutions";
var IL = "ILAB";
var SP = "Spark";
var IT = "IT Innovation";
$(document).ready(function() {
//If Opco is checked, and both ES are checked yes
if (($('input[name="opco"]:checked').length > 0) && ($('#Yes1').is(':checked')) && ($('#Yes2').is(':checked'))) {
var case1 = alert("Primary Recommendation: " + ES + "\n Secondary Recommendation: " + IL + " or " + SP);
}
//If Opco is checked, both ES are checked yes, and ILAB is checked yes
if (($('input[name="opco"]:checked').length > 0) && ($('#Yes1').is(':checked')) && ($('#Yes2').is(':checked')) && ($('#Yes3').is(':checked'))) {
var case2 = alert("Primary Recommendation: " + ES + "\n Secondary Recommendation: " + IL);
}
//If Opco is checked, both ES are checked yes, ILAB is checked no, and Spark is checked yes
if (($('input[name="opco"]:checked').length > 0) && ($('#Yes1').is(':checked')) && ($('#Yes2').is(':checked')) && ($('#No3').is(':checked')) && ($('#Yes4').is(':checked'))) {
var case3 = alert("Primary Recommendation: " + ES + "\n Secondary Recommendation: " + SP);
}
//If Opco is checked, both ES are checked yes, ILAB is checked yes, and Spark is checked yes
if (($('input[name="opco"]:checked').length > 0) && ($('#Yes1').is(':checked')) && ($('#Yes2').is(':checked')) && ($('#Yes3').is(':checked')) && ($('#Yes4').is(':checked'))) {
var case4 = alert("Primary Recommendation: " + ES + "\n Secondary Recommendation: " + IL + " or " + SP);
}
//If Opco is checked, both ES are checked no, and ILAB is checked yes
if (($('input[name="opco"]:checked').length > 0) && ($('#No1').is(':checked')) && ($('#No2').is(':checked')) && ($('#Yes3').is(':checked'))){
var case5 = alert("Primary Recommendation: " + IL + "\n Secondary Recommendation: " + SP);
}
//If Opco is checked, both ES are checked no, ILAB is checked yes, and Spark is checked yes
if (($('input[name="opco"]:checked').length > 0) && ($('#No1').is(':checked')) && ($('#No2').is(':checked')) && ($('#Yes3').is(':checked')) && ($('#Yes4').is(':checked'))){
var case6 = alert("Primary Recommendation: " + IL + " or " + SP);
}
//If Opco is checked, both ES are checked no, Spark is checked yes
if (($('input[name="opco"]:checked').length > 0) && ($('#No1').is(':checked')) && ($('#No2').is(':checked')) && ($('#Yes4').is(':checked'))){
var case7 = alert("Primary Recommendation: " + SP);
}
//If IT is checked yes
if ($('#Yes5').is(':checked')) {
var case8 = alert("Primary Recommendation: " + IT);
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!---------------------------------------------------------Operating Company Question----------------------------------->
<form name="operatingCompany">
<h3>What Operating Company Are You Employeed With?</h3>
<input type="radio" name="opco" id="GPC" value="GPC">GPC</br>
<input type="radio" name="opco" id="APC" value="APC">APC</br>
<input type="radio" name="opco" id="MPC" value="MPC">MPC</br>
<input type="radio" name="opco" id="Gulf" value="Gulf">Gulf</br>
<input type="radio" name="opco" id="SCS" value="SCS">SCS</br>
</br>
<input type="button" value="Display User Selection" onclick=get_opco() />
</form>
<p id="opco_result"> </p>
</br>
<!---------------------------------------------------------Prototyped Question----------------------------------->
<form name="prototyped">
<h3>Has the innovation been prototyped?</h3>
<input type="radio" name="prototyped" id="Yes1" value="Yes">Yes</br>
<input type="radio" name="prototyped" id="No1" value="No">No</br>
</br>
<input type="button" value="Display User Selection" onclick=get_prototype() />
</form>
<p id="prototyped_result"> </p>
</br>
<!--------------------------------------------------------Adopted or Tested Question---------------------------->
<form name="adopted_tested">
<h3>Has the innovation been adobpted or tested?</h3>
<input type="radio" name="adopt" id="Yes2" value="Yes">Yes</br>
<input type="radio" name="adopt" id="No2" value="No">No</br>
</br>
<input type="button" value="Display User Selection" onclick=get_adopt_test() />
</form>
<p id="adopted_tested_result"> </p>
</br>
<!------------------------------------------------------Can it make money Question------------------------------->
<form name="makeMoney">
<h3>Is this a product or service that can make money?</h3>
<input type="radio" name="money" id="Yes3" value="Yes">Yes</br>
<input type="radio" name="money" id="No3" value="No">No</br>
</br>
<input type="button" value="Display User Selection" onclick=get_money() />
</form>
<p id="makeMoney_result"> </p>
</br>
<!---------------------------------------------------Alabama Power Specific Questions----------------------------->
<form name="alabamaPower">
<h3>Does your innovative idea help Alabama Power improve safety, grow revenue, reduce cost, or increase operational efficiency?</h3>
<input type="radio" name="apc" id="Yes4" value="Yes">Yes</br>
<input type="radio" name="apc" id="No4" value="No">No</br>
</br>
<input type="button" value="Display User Selection" onclick=get_alabamaPower() />
</form>
<p id="alabamaPower_result"> </p>
</br>
<!------------------------------------------------Explanation to prior question----------------------------------->
<h3>If yes, please explain</h3>
<textarea id="alabamaPower" rows="8" cols="50">
</textarea> </br>
</br>
<input type="button" value="Display User Input" onclick=textareacapture() />
<p id="result"> </p>
</br>
<!------------------------------------------------IT Specific Question------------------------------------------->
<form name="innovativeTechnology">
<h3>Is your innovation an innovative technology or process that boosts the company's productivity or brings additional value from a vendor relationship?</h3>
<input type="radio" name="innovative" id="Yes5" value="Yes">Yes</br>
<input type="radio" name="innovative" id="No5" value="No">No</br>
</br>
<input type="button" value="Display User Selection" onclick=get_innovative() />
</form>
<p id="innovativeTechnology_result"> </p>
</br>
<input type="button" value="Submit Form" onclick=get_recommendation() />
任何帮助深表感谢!
解决方案
您应该设置一个变量,而不是许多不同的变量。alert()
并且在设置变量时不要调用。
var message;
if ($('input[name="opco"]:checked').length > 0) {
if ($('#Yes1').is(':checked') && $('#Yes2').is(':checked')) {
message = "Primary Recommendation: " + ES + "\n Secondary Recommendation: " + IL + " or " + SP;
}
//If Opco is checked, both ES are checked yes, and ILAB is checked yes
if ($('#Yes1').is(':checked') && $('#Yes2').is(':checked') && $('#Yes3').is(':checked')) {
message = "Primary Recommendation: " + ES + "\n Secondary Recommendation: " + IL;
}
//If Opco is checked, both ES are checked yes, ILAB is checked no, and Spark is checked yes
if ($('#Yes1').is(':checked') && $('#Yes2').is(':checked') && $('#No3').is(':checked') && $('#Yes4').is(':checked')) {
message = "Primary Recommendation: " + ES + "\n Secondary Recommendation: " + SP;
}
//If Opco is checked, both ES are checked yes, ILAB is checked yes, and Spark is checked yes
if ($('#Yes1').is(':checked') && $('#Yes2').is(':checked') && $('#Yes3').is(':checked') && $('#Yes4').is(':checked')) {
message = "Primary Recommendation: " + ES + "\n Secondary Recommendation: " + IL + " or " + SP;
}
//If Opco is checked, both ES are checked no, and ILAB is checked yes
if ($('#No1').is(':checked') && $('#No2').is(':checked') && $('#Yes3').is(':checked')) {
message "Primary Recommendation: " + IL + "\n Secondary Recommendation: " + SP;
}
//If Opco is checked, both ES are checked no, ILAB is checked yes, and Spark is checked yes
if ($('#No1').is(':checked') && $('#No2').is(':checked') && $('#Yes3').is(':checked') && $('#Yes4').is(':checked')) {
message = "Primary Recommendation: " + IL + " or " + SP;
}
//If Opco is checked, both ES are checked no, Spark is checked yes
if ($('#No1').is(':checked') && $('#No2').is(':checked') && $('#Yes4').is(':checked')) {
message = "Primary Recommendation: " + SP;
}
}
//If IT is checked yes
if ($('#Yes5').is(':checked')) {
message = "Primary Recommendation: " + IT;
}
if (message) {
alert(message);
}
推荐阅读
- sql-server - 如何自动化脚本以将数据插入 SQL Server 表
- mysql - 如何每百条记录获取一条记录 - SQLAlchemy,
- css - 通用兄弟选择器 CSS 正在测试但不在网站上
- amazon-web-services - Lambda Docker 容器的完整示例?
- python - 如何将时间字符串转换为每小时数据?
- javascript - 从网络摄像头捕获多张图像 - django + js
- mongodb - 更新 mongoDB 文档中的嵌套值
- r - dplyr 对象未找到我
- python - 将 PyCharm 与 Abaqus 集成
- html - 如何将一个项目向左对齐,另一个中心与 flexbox 对齐?