Check / Uncheck Input Boxes with Jquery

This question already has an answer here:

  • Setting “checked” for a checkbox with jQuery? 38 answers

  • Try this:

    js

      $('document').ready( function() {
        $('.check_boxes').click( function() {
          if ( $('input:checkbox').prop('checked')) {
            $('input:checkbox').prop('checked', true);
          } else {
            $('input:checkbox').prop('checked', false);
          }     
        });
    });
    

    fiddle


    Actually once you click on the checkbox, the first if is always true. Hence all checkeboxes get unchecked (including the one you clicked).

    I assume you wanted this behavior: http://jsfiddle.net/vhLMN/18/

    $('document').ready( function() {
        $('.check_boxes').click( function() {
          if ( !this.checked ) {
            $('.flip_us :checkbox').attr('checked', false);
          } else {
            $('.flip_us :checkbox').attr('checked', true);
          };     
        });
    });
    

    BTW. There is no label attribute on inputs. You should fix this unless you are using some framework that uses it to create actual label tag .


    我认为你需要这个简单的代码:

    $('document').ready( function() {
    $('.check_boxes').click( function() {
      if ( $(':checkbox').prop('checked')) {
        $(':checkbox').prop('checked', true);
      } else {
        $(':checkbox').prop('checked', false);
      }     
     });
    });
    
    链接地址: http://www.djcxy.com/p/12260.html

    上一篇: JavaScript取消选中与动态ID复选框

    下一篇: 使用Jquery检查/取消选中输入框