javascript - 选中其他复选框时取消选中复选框

标签 javascript jquery html checkbox

我尝试了我的代码,但它并不总是有效,我放弃了。

您可以帮助我编写代码,以便在选中其他复选框时取消选中某个复选框吗?

这是我的代码:

JS代码:

$("#1 #checkAll").change(function() {
    if ($("#1 #checkAll").is(':checked')) {
        $("#1 input[type=checkbox]").each(function() {
            $(this).prop("checked", true);
        });
    } else {
        $("#1 input[type=checkbox]").each(function() {
            $(this).prop("checked", false);
        });
    }
});
$("#2 #checkAll2").change(function() {
    if ($("#2 #checkAll2").is(':checked')) {
        $("#2 input[type=checkbox]").each(function() {
            $(this).prop("checked", true);
        });
    } else {
        $("#2 input[type=checkbox]").each(function() {
            $(this).prop("checked", false);
        });
    }
});

$('#c1').on('click', function() {
    var $$ = $(this).next('#checkAll')
    console.log($$.is(':checked'))
    if ($$.is(':checked')) {
        $(this).toggleClass('unchecked checked');
        $('#checkAll').prop('checked', false).change();
    } else {
        $(this).toggleClass('unchecked checked');
        $('#checkAll').prop('checked', true).change();
    }
})
$('#c2').on('click', function() {
    var $$ = $(this).next('#checkAll2')
    if ($$.is(':checked')) {
        $(this).toggleClass('unchecked checked');
        $('#checkAll2').prop('checked', false).change();
    } else {
        $(this).toggleClass('unchecked checked');
        $('#checkAll2').prop('checked', true).change();
    }
})

CSS 代码:

.unchecked {
    background: #1ba1e2; 
    border-color: #1ba1e2;
    height: 70px; 
    font-weight: bold;  
    margin-left:20px; 
    width:200px; 
    font-size: 30px;
}
.checked {
    background: #1c629b; 
    border-color: #1c629b;
    height: 70px; 
    font-weight: bold;  
    margin-left:20px; 
    width:200px; 
    font-size: 30px;
}

HTML 代码:

<div class="container">
    <center>
        <h2 style="color: white; padding-top: 32px; font-size: 50px; font-family: 'Gotham Bold';"><b>Pilih Nominal</b></h2>
        <div style="margin-top: 35px; margin-left: -22px;">
            <form action="" method="POST">
                <input type="hidden" name="sqn" value="20160625110635">
                <input type="hidden" name="saldo" value="Array">
                <input type="hidden" name="mac" value="64:70:02:4a:a7:e4">
                <input type="hidden" name="tid" value="01">
                <input type="hidden" name="msidn" value="6287875230364">
                <input type="hidden" name="typ" value="PREPAID">
                <input type="hidden" name="ip" value="192.168.1.1">
                <input type="hidden" name="cmd" value="prepaid-type">
                <table id="tab1">
                <tr>
                <td id="1">
                    <button type="button" id="c1" class="unchecked">1</button>
                    <input type="checkbox" name="checkAll" id="checkAll" style="display: none;">
                    <input type="checkbox" name="book1" id="book" value="book1">
                    <input type="checkbox" name="book2" id="book" value="book2">
                    <input type="checkbox" name="book3" id="book" value="book3">
                    <input type="checkbox" name="book4" id="book" value="book4">
                    <input type="checkbox" name="book5" id="book" value="book5">
                </td>
                </tr>
                <tr>
                <td id="2">
                    <button type="button" id="c2" class="unchecked">2</button>
                    <input type="checkbox" name="checkAll" id="checkAll2" style="display: none;">
                    <input type="checkbox" name="book1" id="book" value="book1">
                    <input type="checkbox" name="book2" id="book" value="book2">
                    <input type="checkbox" name="book3" id="book" value="book3">
                    <input type="checkbox" name="book4" id="book" value="book4">
                    <input type="checkbox" name="book5" id="book" value="book5">
                </td>
                </tr>
                </table>
                <input type="submit" name="sbm" value="Submit" class="button primary">
            </form>
        </div>
    </center>
</div>

这是我的 fiddle :JSFIDDLE

最佳答案

看看这个:

$("#1 #checkAll").change(function() {
      if ($("#1 #checkAll").is(':checked')) {
        $("#1 input[type=checkbox]").each(function() {
          $(this).prop("checked", true);
        });
        //When you check all #1 checkboxes, 
        //you uncheck all #2 checkboxes
        $("#2 input[type=checkbox]").each(function() {
          $(this).prop("checked", false);
        });
      } else {
        $("#1 input[type=checkbox]").each(function() {
          $(this).prop("checked", false);
        });
      }
    });
    $("#2 #checkAll2").change(function() {
      if ($("#2 #checkAll2").is(':checked')) {
        $("#2 input[type=checkbox]").each(function() {
          $(this).prop("checked", true);
        });
        //When you check all #2 checkboxes, 
        //you uncheck all #1 checkboxes
        $("#1 input[type=checkbox]").each(function() {
          $(this).prop("checked", false);
        });
      } else {
        $("#2 input[type=checkbox]").each(function() {
          $(this).prop("checked", false);
        });
      }
    });

    $('#c1').on('click', function() {
      var $$ = $(this).next('#checkAll')
      console.log($$.is(':checked'))
      if ($$.is(':checked')) {
        $(this).toggleClass('unchecked checked');
        $('#checkAll').prop('checked', false).change();
      } else {
        $(this).toggleClass('unchecked checked');
        //when you click #c1, you remove class checked to #c2
        $("#c2").removeClass('checked');
        $("#c2").addClass('unchecked');
        $('#checkAll').prop('checked', true).change();
      }
    })
    $('#c2').on('click', function() {
      var $$ = $(this).next('#checkAll2')
      if ($$.is(':checked')) {
        $(this).toggleClass('unchecked checked');
        $('#checkAll2').prop('checked', false).change();
      } else {
        $(this).toggleClass('unchecked checked');
        //when you click #c2, you remove class checked to #c1
        $("#c1").removeClass('checked');
        $("#c1").addClass('unchecked');
        $('#checkAll2').prop('checked', true).change();
      }
    })
.unchecked {
		background: #1ba1e2; border-color: #1ba1e2;
		 height: 70px; font-weight: bold;  margin-left:20px; width:200px; font-size: 30px;
		}
.checked {background: #1c629b; border-color: #1c629b;
		 height: 70px; font-weight: bold;  margin-left:20px; width:200px; font-size: 30px;
		}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
  <center>
    <h2 style="color: white; padding-top: 32px; font-size: 50px; font-family: 'Gotham Bold';"><b>Pilih Nominal</b></h2>
    <div style="margin-top: 35px; margin-left: -22px;">

      <form action="" method="POST">
        <input type="hidden" name="sqn" value="20160625110635">
        <input type="hidden" name="saldo" value="Array">
        <input type="hidden" name="mac" value="64:70:02:4a:a7:e4">
        <input type="hidden" name="tid" value="01">
        <input type="hidden" name="msidn" value="6287875230364">
        <input type="hidden" name="typ" value="PREPAID">
        <input type="hidden" name="ip" value="192.168.1.1">
        <input type="hidden" name="cmd" value="prepaid-type">
        <table id="tab1">
          <tr>
            <td id="1">
              <button type="button" id="c1" class="unchecked">
                1
              </button>
              <input type="checkbox" name="checkAll" id="checkAll" style="display: none;">
              <input type="checkbox" name="book1" id="book" value="book1">
              <input type="checkbox" name="book2" id="book" value="book2">
              <input type="checkbox" name="book3" id="book" value="book3">
              <input type="checkbox" name="book4" id="book" value="book4">
              <input type="checkbox" name="book5" id="book" value="book5">
            </td>
          </tr>
          <tr>
            <td id="2">
              <button type="button" id="c2" class="unchecked">
                2
              </button>
              <input type="checkbox" name="checkAll" id="checkAll2" style="display: none;">
              <input type="checkbox" name="book1" id="book" value="book1">
              <input type="checkbox" name="book2" id="book" value="book2">
              <input type="checkbox" name="book3" id="book" value="book3">
              <input type="checkbox" name="book4" id="book" value="book4">
              <input type="checkbox" name="book5" id="book" value="book5">
            </td>
          </tr>
        </table>
        <input type="submit" name="sbm" value="Submit" class="button primary">
      </form>
    </div>

关于javascript - 选中其他复选框时取消选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116818/

相关文章:

javascript - 停止滚动 anchor 击

javascript - 使用浏览器后退按钮时如何强制重新加载页面?

php - 使用 jQuery 函数从数据库下载文件

javascript - 当窗口大小更改时,将根 div 中心保持在页面上

html - 标签后的空格文本框

jQuery "stacking"div(像乐高一样)

javascript - 使用 Javascript 进行编程梯度停止

javascript - 无法使用 gulp-inline-css 将 CSS 内联到 html 文件中的样式标签

javascript - 查询时并不总是提供参数

javascript - 使用 jQuery 和 Ajax 提交表单