如何在选中复选框时打开 bootstrap 模式(当勾选复选框时)
How to open a bootstrap modal when checked a chec-box (when a tick is placed on a check box)
我有一个自定义复选框。
我想要的是在选中复选框时打开 bootstrap 模式。
我希望 bootstrap 模式像单击按钮时一样打开。
希望你明白我的问题。
我该怎么做。
HTML 代码
/*
----------------------------------------------
CHECKBOX
----------------------------------------------
*/
/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked)+label,
[type="checkbox"]:checked+label {
position: relative;
padding-left: 25px;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:checked+label:before {
content: '';
position: absolute;
left: 0;
top: 0px;
width: 20px;
height: 20px;
//border: 1px solid #aaa;
background: #09ad7e;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
[type="checkbox"]:not(:checked)+label:before {
content: '';
position: absolute;
left: 0;
top: 0px;
width: 20px;
height: 20px;
//border: 1px solid #fff;
background: #eee;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:checked+label:after {
content: '✔';
position: absolute;
top: 0;
left: 4px;
font-size: 14px;
color: #f8f8f8;
transition: all .2s;
}
[type="checkbox"]:not(:checked)+label:after {
content: '✔';
position: absolute;
top: 0;
left: 4px;
font-size: 14px;
color: #ddd;
transition: all .2s;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked)+label:after {
opacity: 1;
transform: scale(1);
}
[type="checkbox"]:checked+label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked)+label:before,
[type="checkbox"]:disabled:checked+label:before {
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked+label:after {
color: #999;
}
[type="checkbox"]:disabled+label {
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus+label:before,
[type="checkbox"]:not(:checked):focus+label:before {
outline: none !important;
}
/* hover style just for information */
label:hover:before {
//border: 1px solid #4778d9!important;
}
[type="checkbox"]:not(:checked)+label {
color: #ddd;
}
<input type="checkbox" id="test7" checked="checked" />
<label for="test7">Custom Avalability</label>
<br /><br />
<input type="checkbox" id="test6" />
<label for="test6">Manual</label>
这是 JS fiddle。如果您还想更改它,请告诉我。
http://jsfiddle.net/k3aogpv0/5/
具有简单的自定义数据属性:
<input type="checkbox" id="test7" checked="checked" data-toggle="modal" data-target="#myModal" />
如果您不希望未选中
,则可以使用 Javascript
$("[id^=test]").on("change", function(e){
if(e.target.checked){
$('#myModal').modal();
}
});
您可以检查 input[type="checkbox"]
更改时是否检查了输入并显示模态。
$('input[type="checkbox"]').on('change', function(e){
if(e.target.checked){
$('#myModal').modal();
}
});
/*
----------------------------------------------
CHECKBOX
----------------------------------------------
*/
/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative;
padding-left: 25px;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 0px;
width: 20px; height: 20px;
//border: 1px solid #aaa;
background: #09ad7e;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
[type="checkbox"]:not(:checked) + label:before {
content: '';
position: absolute;
left:0; top: 0px;
width: 20px; height: 20px;
//border: 1px solid #fff;
background: #eee;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 0; left: 4px;
font-size: 14px;
color: #f8f8f8;
transition: all .2s;
}
[type="checkbox"]:not(:checked) + label:after {
content: '✔';
position: absolute;
top: 0; left: 4px;
font-size: 14px;
color: #ddd;
transition: all .2s;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after {
opacity: 1;
transform: scale(1);
}
[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked + label:after {
color: #999;
}
[type="checkbox"]:disabled + label {
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before {
outline: none !important;
}
/* hover style just for information */
label:hover:before {
//border: 1px solid #4778d9!important;
}
[type="checkbox"]:not(:checked) + label {
color: #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<input type="checkbox" id="test7" checked="checked"/>
<label for="test7">Custom Avalability</label>
<br /><br />
<input type="checkbox" id="test6"/>
<label for="test6">Manual</label>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Checkbox is checked
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<input type="checkbox" id="test7" checked="checked"/>
<label for="test7">Custom Avalability</label>
<br /><br />
<input type="checkbox" id="test6"/>
<label for="test6">Manual</label>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Checkbox is checked
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
我有一个自定义复选框。 我想要的是在选中复选框时打开 bootstrap 模式。 我希望 bootstrap 模式像单击按钮时一样打开。 希望你明白我的问题。 我该怎么做。
HTML 代码
/*
----------------------------------------------
CHECKBOX
----------------------------------------------
*/
/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked)+label,
[type="checkbox"]:checked+label {
position: relative;
padding-left: 25px;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:checked+label:before {
content: '';
position: absolute;
left: 0;
top: 0px;
width: 20px;
height: 20px;
//border: 1px solid #aaa;
background: #09ad7e;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
[type="checkbox"]:not(:checked)+label:before {
content: '';
position: absolute;
left: 0;
top: 0px;
width: 20px;
height: 20px;
//border: 1px solid #fff;
background: #eee;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:checked+label:after {
content: '✔';
position: absolute;
top: 0;
left: 4px;
font-size: 14px;
color: #f8f8f8;
transition: all .2s;
}
[type="checkbox"]:not(:checked)+label:after {
content: '✔';
position: absolute;
top: 0;
left: 4px;
font-size: 14px;
color: #ddd;
transition: all .2s;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked)+label:after {
opacity: 1;
transform: scale(1);
}
[type="checkbox"]:checked+label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked)+label:before,
[type="checkbox"]:disabled:checked+label:before {
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked+label:after {
color: #999;
}
[type="checkbox"]:disabled+label {
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus+label:before,
[type="checkbox"]:not(:checked):focus+label:before {
outline: none !important;
}
/* hover style just for information */
label:hover:before {
//border: 1px solid #4778d9!important;
}
[type="checkbox"]:not(:checked)+label {
color: #ddd;
}
<input type="checkbox" id="test7" checked="checked" />
<label for="test7">Custom Avalability</label>
<br /><br />
<input type="checkbox" id="test6" />
<label for="test6">Manual</label>
这是 JS fiddle。如果您还想更改它,请告诉我。
http://jsfiddle.net/k3aogpv0/5/
具有简单的自定义数据属性:
<input type="checkbox" id="test7" checked="checked" data-toggle="modal" data-target="#myModal" />
如果您不希望未选中
,则可以使用 Javascript $("[id^=test]").on("change", function(e){
if(e.target.checked){
$('#myModal').modal();
}
});
您可以检查 input[type="checkbox"]
更改时是否检查了输入并显示模态。
$('input[type="checkbox"]').on('change', function(e){
if(e.target.checked){
$('#myModal').modal();
}
});
/*
----------------------------------------------
CHECKBOX
----------------------------------------------
*/
/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative;
padding-left: 25px;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 0px;
width: 20px; height: 20px;
//border: 1px solid #aaa;
background: #09ad7e;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
[type="checkbox"]:not(:checked) + label:before {
content: '';
position: absolute;
left:0; top: 0px;
width: 20px; height: 20px;
//border: 1px solid #fff;
background: #eee;
border-radius: 3px;
//box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 0; left: 4px;
font-size: 14px;
color: #f8f8f8;
transition: all .2s;
}
[type="checkbox"]:not(:checked) + label:after {
content: '✔';
position: absolute;
top: 0; left: 4px;
font-size: 14px;
color: #ddd;
transition: all .2s;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after {
opacity: 1;
transform: scale(1);
}
[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked + label:after {
color: #999;
}
[type="checkbox"]:disabled + label {
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before {
outline: none !important;
}
/* hover style just for information */
label:hover:before {
//border: 1px solid #4778d9!important;
}
[type="checkbox"]:not(:checked) + label {
color: #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<input type="checkbox" id="test7" checked="checked"/>
<label for="test7">Custom Avalability</label>
<br /><br />
<input type="checkbox" id="test6"/>
<label for="test6">Manual</label>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Checkbox is checked
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<input type="checkbox" id="test7" checked="checked"/>
<label for="test7">Custom Avalability</label>
<br /><br />
<input type="checkbox" id="test6"/>
<label for="test6">Manual</label>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Checkbox is checked
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>