【问题标题】:Comparing all If statements first BEFORE displaying an output在显示输出之前先比较所有 If 语句
【发布时间】:2018-12-28 01:12:20
【问题描述】:

我以为我已经弄清楚了逻辑;但是,我得到的输出是第一个被发现为真的 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() />

非常感谢任何帮助!

【问题讨论】:

  • 你为什么要一遍又一遍地检查同一件事if ($('input[name="opco"]:checked').length &gt; 0)?做一次,然后把其他所有东西都放进去。
  • 您应该将警报消息分配给每个if 块中的变量。然后,您可以在最后使用该消息调用 alert 一次。
  • 而不是嵌套if 块,使用&amp;&amp;if (x) if (y) if (z) ... 应该是 if (x &amp;&amp; y &amp;&amp; z) ...
  • 除了第一个 if 之外,还有更多重复。如果您有if (x) if (y) {action 1} 后跟if (x) if (y) if (z) {action 2},则应将它们组合成if (x &amp;&amp; y) { action 1; if (z) { action 2 } }
  • 另外,没有必要使用$(document).readycheck_recommendation() 在用户点击时被调用,在文档准备好之前他们不能这样做。

标签: javascript jquery html radio-button switch-statement


【解决方案1】:

您应该设置一个变量,而不是许多不同的变量。并且在设置变量时不要调用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);
}

【讨论】:

    猜你喜欢
    • 2011-12-12
    • 1970-01-01
    • 2022-11-16
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2016-07-17
    相关资源
    最近更新 更多