if else语句使用多个单选按钮

时间:2014-04-16 20:50:13

标签: javascript jquery input radio-button getelementsbyname

我想要做的是创建一个有三个是或否问题的调查问卷。如果所有三个问题都是“是”'选中,然后我想要一个警报。如果任何问题有“不”。选中,然后我想要一个特定于该问题的警报。我以为我有它,但我错过了什么,某个地方。你能帮忙吗?


<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>

1 个答案:

答案 0 :(得分:0)

你几乎得到了它!您只是缺少一个括号来关闭点击功能。你的最后一行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