javascript - HTML/Javascript && If 语句中的条件

标签 javascript html if-statement radio-button

我确信对此有一个非常简单的修复方法,而且我很可能没有以最有效的方式做到这一点。

我正在尝试创建一个 if 语句,该语句将检查是否在多个问题中选择了多个单选按钮。如果选择它们,我希望弹出一个带有特定消息的警报框。可能有很多选择组合,所以我假设有很多 if/else 语句(?)

有问题的 JavaScript 位于底部。

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Spigit Input Form</title>
  <meta name="description" content="Spigit Input Form">
  <meta name="author" content="SitePoint">

  <link rel="stylesheet" href="css/styles.css?v=1.0">
  <link rel="stylesheet" href="Project_File_CSS.css">

  <!--[if lt IE 9]>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
  <![endif]-->

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="jquery_spigit.js"></script>
</head>

<body>

  <!--<script src="js/scripts.js"></script>
  <script src="New_File_JS.js"></script>-->
  <script type="text/javascript" src="selection_storage.js"></script>
  <!--<script type="text/javascript" src="recommendation_logic.js"></script>-->

<!---------------------------------------------------------Operating Company Question----------------------------------->
<form name="operatingCompany">
<h3>What Operating Company Are You Employeed With?</h3>
<input type="radio" name="opco" value="GPC" >GPC</br>
<input type="radio" name="opco" value="APC" >APC</br>
<input type="radio" name="opco" value="MPC" >MPC</br>
<input type="radio" name="opco" value="Gulf" >Gulf</br>
<input type="radio" name="opco" 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" value="Yes" >Yes</br>
<input type="radio" name="prototyped" 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" value="Yes" >Yes</br>
<input type="radio" name="adopt" 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" value="Yes" >Yes</br>
<input type="radio" name="money" 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 Question----------------------------->
<h3>What is your innovative idea to help Alabama Power improve safety, grow revenue, reduce cost, or increase operational efficiency?</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" value="Yes" >Yes</br>
<input type="radio" name="innovative" 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() />

<script>
function get_recommendation(){
        if((document.operatingCompany.opco[0,1,2,3,4].checked) && (document.prototyped.prototyped[0,1].checked) && (document.adopted_tested.adopt[0,1].checked))
        {
            alert("Everyday Solutions");
        }       
}
</script>

</body>
</html>

最佳答案

使用此测试来确定是否已选中任何单选按钮:

($(":checked").length > 0)

如果您只对页面上的单选按钮或复选框的子集感兴趣,则可以优化选择器。

关于javascript - HTML/Javascript && If 语句中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51405691/

相关文章:

javascript - 使用 if else 显示和隐藏标签

javascript - 在刷新页面之前不显示 React-Tooltip

javascript - 创建混合高度合理的标题?

javascript - 如何创建在显示过滤器时自行扩展的搜索框?

java - 由于If语句无法登录

MySQL IF NOT NULL,则显示 1,否则显示 0

javascript - 过渡/动画后CSS触发javascript

javascript - 从字符串模板获取通配符名称

javascript - 无法弄清楚如何根据日期自动显示文本内容

html - 我的 isp ftp 的背景图像完整路径 url?