jQuery evaluate if checkbox is checked and contains the class true

This question already has an answer here:

  • How to check whether a checkbox is checked in jQuery? 56 answers

  • $('.multiplesubmit').click(function () {
      var correctAnswers = $(this).next('ul').children('li').filter(function () {
        return $(this).hasClass('True') && $(this).find('input[type="checkbox"]:checked').length>0;
      });
      if(correctAnswers.length>0) {
        alert('found')
      }
    });
    

    JSFiddle

    Update

    As per comments

    $('.multiplesubmit').click(function () {
      var correctAnswers = $(this).next('ul').children('li').filter(function () {
        return $(this).hasClass('True') && $(this).find('input[type="checkbox"]:checked').length>0;
      });
      var wrongAnswers = $(this).next('ul').children('li').filter(function () {
        return $(this).hasClass('False') && $(this).find('input[type="checkbox"]:checked').length>0;
      });
      if (correctAnswers.length>0 && wrongAnswers.length<1) {
        alert('found')
      }
    });
    

    JSFiddle


    try this:

    I change the color of text to red for all incorrect, and green for correct, but I left comments so you can do whatever you like for incorrect and correct answers

    $('.multiplesubmit').click(function() {
        $.each('input:checkbox[name=checkme]', function(){
        if( $(this).is(':checked'){
            if( $(this).hasClass('True'){       
               // CHECKED - CORRECT
               $(this).parent().css({ 'color' : 'green' });
             }
             else{
               // CHECKED - INCORRECT
               $(this).parent().css({ 'color' : 'red' });
             }
        }
        else{
          if( $(this).hasClass('True'){       
            // UN-CHECKED - INCORRECT
            $(this).parent().css({ 'color' : 'red' });
          }
            else{
           // UNCHECKED - CORRECT
            $(this).parent().css({ 'color' : 'green' });
          }
    
        });
    });
    

    This will check each checkbox individually, since you could have multiple that are checked and have the True class.

    $(document).ready(function() {
      $('.multiplesubmit').click(function() {
        multipleChoiceCheck();
      });
    });
    
    function multipleChoiceCheck() {
      $(".multiplechoice_answergroup").find("input[type=checkbox]").each(function() {
        var $this = $(this);
        if($this.prop("checked") && $this.parent().hasClass("True")) {
          alert($this.val());
        }
      });
    }
    

    I added values to your checkboxes to differentiate the answers:

    <button class="multiplesubmit">Check Answer</button>
    <ul class="multiplechoice_answergroup">
        <li class="multiplechoice_answer True"><input class="checkbox" type="checkbox" name="checkbox" value="Answer 1"> Answer 1</li>
        <li class="multiplechoice_answer False"><input class="checkbox" type="checkbox" name="checkbox" value="Answer 2"> Answer 2</li>
        <li class="multiplechoice_answer False"><input class="checkbox" type="checkbox" name="checkbox" value="Answer 3"> Answer 3</li>
        <li class="multiplechoice_answer True"><input class="checkbox" type="checkbox" name="checkbox" value="Answer 4"> Answer 4</li>
    </ul>
    

    JS Fiddle Example

    链接地址: http://www.djcxy.com/p/9530.html

    上一篇: 从交换机获得价值

    下一篇: jQuery评估复选框是否被选中并包含true类