在iCheck的复选框上显示并隐藏另一个复选框已选中并取消选中状态

时间:2016-03-07 07:31:07

标签: javascript jquery html css icheck

这是我的代码

Icheck复选框css类映射代码

<script>
$(function () {
    //Initialize Select2 Elements
    $(".select2").select2();        
    //Flat red color scheme for iCheck
    $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
        checkboxClass: 'icheckbox_flat-red',
        radioClass: 'iradio_flat-red'
    });

});

用于显示和隐藏第二个复选框的Jquery

<script>
$("#ch_bx_haveparent").iCheck('toggle', function () {
    $("#ch_bx_haveparent").on('ifChecked', function (event) {
        alert('ch_bx_status Checkbox Shown');
        $("#ch_bx_status").show();
    });
    $("#ch_bx_haveparent").on('ifUnchecked', function (event) {
        alert('ch_bx_status Checkbox  hidden');
        $("#ch_bx_status").hide();
    });
});

click here for Icheck Checkbox

HTML代码

<div class="modal" id="menuiconmodal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Menu</h4>
          </div>
          <div class="modal-body"> 

       <div class="form-group"><input type="checkbox" class="flat-red" id="ch_bx_haveparent"/> 
       // Input tag for checkbox that will be shown and hidden
       <div class="form-group"><input type="checkbox" class="flat-red" runat="server" id="ch_bx_status" /></div>
       </div>
          </div>
          <div class="modal-footer">

            <a href="javascript:;" id="sucess" class="btn btn-primary" data-dismiss="modal">Save</a>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>  
    <!-- /.modal -->

警告正在填充,但隐藏和显示无效。

3 个答案:

答案 0 :(得分:0)

$("#ch_bx_haveparent").change(function() {
  if ($("#ch_bx_haveparent").is(':checked')) {
    $("#ch_bx_status").show();
  } else {
    $("#ch_bx_status").hide();
  }

}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="modal" id="menuiconmodal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title">Menu</h4>
      </div>
      <div class="modal-body">

        <div class="form-group">
          <input type="checkbox" class="flat-red" id="ch_bx_haveparent" />// Input tag for checkbox that will be shown and hidden
          <div class="form-group">
            <input type="checkbox" class="flat-red" runat="server" id="ch_bx_status" />
          </div>
        </div>
      </div>
      <div class="modal-footer">

       
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

试试这种方式

答案 1 :(得分:0)

$('#ch_bx_haveparent').change(function () {
    if ($(this).attr("checked")) {
        $("#ch_bx_status").show();
        return;
    }
    $("#ch_bx_status").hide();
});

答案 2 :(得分:0)

**使用jquery的addClass()和removeClass()找到解决方案icheck复选框隐藏并显示在**的方法

CSS代码

.call
    {
        display:none;
    }

Jquery代码

<script>
$("#ch_bx_haveparent").iCheck('toggle', function () {
    $("#ch_bx_haveparent").on('ifChecked', function (event) {
        $("#parentmenu").removeClass("call"); // hide

    });
    $("#ch_bx_haveparent").on('ifUnchecked', function (event) {
        $("#parentmenu").addClass("call"); // shown
    });
});

**其中div id =“parentmenu”**