2015-12-02 101 views
0

美好的一天!要使用复选框属性(“checked”,false/true)隐藏/显示元素。所以我创建了这个例子来解释我想要的。 https://jsfiddle.net/9LzLm9hx/4/ 尝试选中并取消选中类别复选框,然后按下按钮hide/show。当这个复选框是false它隐藏,但我不能让他们true之后。谢谢。使用复选框隐藏并显示元素

+4

检查更新的小提琴[这里](https://jsfiddle.net/9LzLm9hx/6/) – Akshay

+0

不工作一样previuos之一 – Dilip

回答

2

你可以在你的元素使用相同的idclass名字,你能总结一下以下几点:

$('.js-ok').on('click', function() { 
 
    //iterate through checkboxes 
 
    $(".custom-checkbox :checkbox").each(function() { 
 
    //toggle specific element according to the checkbox status 
 
    $("." + $(this).attr("id")).toggle($(this).prop("checked")); 
 
    }) 
 
});
#common { 
 
    margin-left: 20px; 
 
} 
 
#common + label + span { 
 
    font-size: 21px; 
 
    color: red; 
 
} 
 
.col-xs-3 { 
 
    margin-top: 29px; 
 
    border: 1px solid black; 
 
} 
 
.category { 
 
    color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
<div class="modal-body row col-xs-12"> 
 
    <div class="col-xs-8 common-inputs"> 
 
    <form role="form" class="left-checkbox-container col-xs-12"> 
 
     <div class="custom-checkbox checkbox-title"> 
 
     <input type="checkbox" id="common" /> 
 
     <label for="common"></label> <span>Common</span> 
 
     </div> 
 
    </form> 
 
    <form role="form" class="left-checkbox-container col-xs-5"> 
 
     <div class="custom-checkbox"> 
 
     <input type="checkbox" id="post-id" /> 
 
     <label for="post-id"></label> <span>ID</span> 
 
     </div> 
 
     <div class="custom-checkbox"> 
 
     <input type="checkbox" id="active" name="check" /> 
 
     <label for="activation"></label> <span>Active</span> 
 
     </div> 
 
     <div class="custom-checkbox"> 
 
     <input type="checkbox" id="info" name="check" /> 
 
     <label for="information"></label> <span>info</span> 
 
     </div> 
 
    </form> 
 
    <form role="form" class="left-checkbox-container col-xs-5"> 
 
     <div class="custom-checkbox"> 
 
     <input type="checkbox" id="onOff" /> 
 
     <label for="on-off"></label> <span>on/off</span> 
 
     </div> 
 
     <div class="custom-checkbox"> 
 
     <input type="checkbox" id="socialNet" name="check" /> 
 
     <label for="social-network"></label> <span>social</span> 
 
     </div> 
 
     <div class="custom-checkbox"> 
 
     <input type="checkbox" id="category" name="check" /> 
 
     <label for="category"></label> <span>category</span> 
 
     </div> 
 
    </form> 
 
    <button class='btn btn-primary js-ok'>hide/show category</button> 
 

 
    </div> 
 

 
    <div class="container"> 
 
    <div class="row"> 
 
     <div class="col-xs-12"> 
 
     <div class='col-xs-3 post-id'>id</div> 
 
     <div class='col-xs-3 active'>active</div> 
 
     <div class='col-xs-3 info'>info</div> 
 
     <div class='col-xs-3 onOff'>on/off</div> 
 
     <div class='col-xs-3 socialNet'>social</div> 
 
     <div class='col-xs-3 category'>category</div> 
 
     </div> 
 
    </div> 
 
    </div>

参考

.toggle()

.attr()

0

.attr("checked", false)是对元素分配假的,并将其返回,计算结果为真(存在),你要比较的属性,为此,您可以:

$('#category').attr("checked") === false

0

如果你想检查是否选中复选框或不使用此代码$('#category').is(':checked'),该代码$('#category').attr("checked", false)集复选框ATTR为false,但返回,所以你当前的代码只是一直不选中#category复选框和隐藏.category元素