JQuery多选

时间:2012-03-14 02:43:14

标签: jquery checkbox multiple-choice

我正在尝试创建一个单选题。

在下面的示例中,我希望选项1和3是正确的答案。单击提交按钮时,我只想运行 if 语句以查看是否选中了选项1和3复选框,如果是,则切换 correct1 div。否则,如果选项1和3未选择 ,则切换 incorrect1 div。

在Head标签中我会有

<script type="text/javascript">
    function toggleDiv2(divId) {
    $("#"+divId).toggle("slow");
            }
</script>

然后在身体里我会:

<div class="control-group">

            <label class="control-label" for="optionsCheckboxList">Check your answers</label>
            <div class="controls">
              <label class="checkbox">
                <input type="checkbox" id="option1" name="optionsCheckboxList1" value="option1">
                <strong>Option One</strong> this is a correct answer. Select this one.
              </label>
              <label class="checkbox">
                <input type="checkbox" id="option2" name="optionsCheckboxList2" value="option2">
                <strong>Option two</strong>  this is an incorrect answer. Do not select this one.
              </label>
              <label class="checkbox">
                <input type="checkbox" id="option3" name="optionsCheckboxList3" value="option3">
                <strong>Option three</strong>  This is a correct answer. Select this one.
              </label>
              <p class="help-block"><strong>Hint:</strong> Add a hint if you would like. This is optional.</p>
          <br>


        <a href="javascript:toggleDiv2('correct1');" class="btn btn-primary">Submit</a>

          <br><br>

            <div id="correct1" class="alert alert-success" style="display: none">
            Well Done! You correctly answered this question!
            </div><!-- end correct1 -->

            <div id="incorrect1" class="alert alert-error" style="display: none">
            Oh snap! Please try again.
            </div><!-- end incorrect1 -->
            </div><!-- end control-group -->

不确定这是否重要,但这是页面链接到的jquery:

<script src="html/assets/js/jquery-ui-1_8_18custom/js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="html/assets/js/jquery-ui-1_8_18custom/js/jquery-ui-1.8.18.custom.min.js" type="text/javascript" ></script>

1 个答案:

答案 0 :(得分:0)

这让它有效......

<script type="text/javascript">
   function toggleDiv2() {
   if($("#option1").is(':checked') && $("#option3").is(':checked'))
   $("#correct1").toggle("slow");
    else
   $("#incorrect1").toggle("slow");
   }
</script>