0

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

在下面的示例中,我希望选项 1 和 3 是正确答案。单击提交按钮时,我只想运行一个if语句以查看是否选择了选项 1 和 3 复选框,如果是,则切换正确的 1 div。否则,如果选择选项 1 和 3,则切换不正确的 1 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>
4

1 回答 1

0

这让它工作......

<script type="text/javascript">
   function toggleDiv2() {
   if($("#option1").is(':checked') && $("#option3").is(':checked'))
   $("#correct1").toggle("slow");
    else
   $("#incorrect1").toggle("slow");
   }
</script>
于 2012-03-15T01:03:02.423 回答