【问题标题】:if else statements using multiple radio buttons使用多个单选按钮的 if else 语句
【发布时间】:2014-05-31 22:34:28
【问题描述】:

我想做的是创建一个包含三个是或否问题的问卷。如果所有三个问题都选择了“是”,那么我想要一个警报。如果有任何问题选择了“否”,那么我需要针对该问题的警报。我以为我拥有它,但我在某个地方错过了一些东西。你能帮忙吗?


<script type="text/javascript">
    $(document).ready(function() {
    $('#getAnswersButton').click(function(Submit) {
        var groceryList = document.getElementsByName('groceryList');
        var equipTimeline= document.getElementsByName('equipTimeline');
        var whatTime= document.getElementsByName('whatTime');

    if (groceryList[0].checked && equipTimeline[0].checked && whatTime[0].checked) {
        alert("Great!  You’re all set! Let’s start cooking!  Just click the 'Next' button to continue.");

    } else if (groceryList[1].checked) {
        alert("Please select the 'Grocery List' link above and review the list to see what you may have missed.");

    } else if (equipTimeline[1].checked) {
        alert("Please select the 'Equip/Timeline' link above and make sure you have all of your tools.");                               

    } else if (whatTime[1].checked) {
        alert("Please select the 'Equip/Timeline' link above and make sure you have all of your tools.");                               
        }        
    }
});
</script>



<div>
    <form>
        <ol>
            <li><label for="ingredients">Do you have all of the ingredients listed on the grocery list?</label></li>                
            <p>
                <input type="radio" id="ingredients" name="groceryList" value="Yes" /> Yes
                <input type="radio" id="ingredients" name="groceryList" value="No" /> No
            </p><br>

            <li><label for="equip">Did you gather up all of the utensils, appliances, and tools that you will need to use?</label></li>             
            <p>
                <input type="radio" id="equip" name="equipTimeline" value="Yes" /> Yes
                <input type="radio" id="equip" name="equipTimeline" value="No" /> No
            </p><br>

            li><label for="timeToEat">What time are you preparing to serve dinner?  You need to subtract a <b>minimum</b> of 3 hours from that time.  Keep an eye on the clock! <i>(For example: if you are serving dinner at 7pm, then you should start preparing everything at 4pm.)</i></label></li>             
            <p>
                <input type="radio" id="timeToEat" name="whatTime" value="Yes" /> Yes
                <input type="radio" id="timeToEat" name="whatTime" value="No" /> No
            </p><br>
        </ol>
    </form>
</div>

【问题讨论】:

  • 您的问题在哪里?问题通常是以与编程有关的问号结尾的句子。现在你只有一个问题:“你能帮忙吗?”。答案是肯定的!请将您的规格发布到“do-my-work-for-me.com”网站,然后阅读:sscce.org 请提供简短、自包含、正确(可编译)的示例。并就该代码提出一个简短而具体的问题。
  • 我很抱歉没有像你这么了解。我对此完全陌生,并试图自己解决。根据我所拥有的以及我在参考书中看到的内容,我认为我的代码可以工作。如果我和你一样理解这一切,我就会找出问题所在。话虽如此,我想弄清楚为什么我上面的脚本没有给我想要的结果。如果我对脚本的了解足以知道要求什么,我会给出非常 SSCCE。如果这么麻烦回答,请不要。

标签: javascript jquery input radio-button getelementsbyname


【解决方案1】:

你几乎明白了!你只是缺少一个括号来关闭点击功能。您最后的 javascript 行应该是:

    ... } else if (whatTime[1].checked) {
    alert("Please select the 'Equip/Timeline' link above and make sure you have all of your tools.");                               
    }        
  }); //This parenthesis was missing!
});
</script>

Demo code

【讨论】:

  • 我将您的代码复制/粘贴到 JSFiddle 并添加了额外的 });我还添加了触发事件的按钮。 jsfiddle.net/sp2v5
  • Chavakane,感谢您帮助我!当我对这一切如此陌生并且不了解我所看到的一切时,这令人沮丧。我添加了额外的 });但是当我测试它时仍然无法得到我正在寻找的结果。我的第二行编码方式有什么不正确的吗? $('#getAnswersButton').click(function(Submit) { 我一直认为是那个,或者在我有变量[#}的语句中。你觉得这些有什么问题吗?
  • 哇,它可以在您的 JSFiddle 中使用!当我刚刚将它添加到我的代码中时它没有。让我复制粘贴试试看。
猜你喜欢
  • 2016-03-19
  • 2023-03-12
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多