[英]Close modal in specific situations bootstrap4
I want to disable modal. 我想禁用模式。 if there're more than 1 checkboxes that checked and allow modal when only 1 checkboxes that checked. 如果选中的复选框超过1个,并且仅选中1个复选框则允许模式。 At now, when i clicked button modal it just pop-up without error. 现在,当我单击模式按钮时,它会弹出而没有错误。 but, it just allow always. 但是,它总是允许。
<input type="checkbox" id="check-all">
<input type="checkbox" class="check">
<input type="checkbox" class="check">
<input type="checkbox" class="check">
<button id="btn-edit" type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
EDIT
</button>
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">TEMPLATE MESSAGE</h5>
<button id="btn-close" type="button" class="close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="">
<div class="form-group">
<textarea id="value-message" type="text" class="form-control" name="template-message" placeholder="Message"></textarea>
</div>
<div class="row">
<div class="col">
<button id="btn-save" class="btn btn-primary">Save</button>
</div>
<div class="col text-right">
<span id="length-message"></span><span> / 160</span>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Jquery jQuery的
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
$("btn-edit").click(function(){
var checkLength = 0;
for(var i = 0; $('.check').length; i++){
checkLength += 1;
}
if(checkLength > 1){
$(".modal").on("shown.bs.modal", function(){
$(".modal").modal("hide");
});
}else{
$(".modal").modal("show");
}
});
FIRST: btn-edit
is an ID you should call it with #btn-edit
首先: btn-edit
是一个ID,您应该使用#btn-edit
对其进行命名
SECOND: for getting checked checkboxes length for specific class you should use: 第二:要获取特定类的复选框长度,应使用:
$('input:checkbox.check:checked')
THIRD: You don't need a for loop for this since the code above will give you the amount of checked checkboxes. 第三:您不需要for循环,因为上面的代码将为您提供选中复选框的数量。
FORTH: Here is a sample near to your code: FORTH:这是靠近您的代码的示例:
$("#btn-edit").click(function(){ if($('input:checkbox.check:checked').length > 1){ $(".info").html("More than 1! The modal will not show"); $(".modal").hide(); } else if ($('input:checkbox.check:checked').length == 1){ $(".info").html("ONLY ONE CHECKBOX CHECKED. The modal will show!"); $(".modal").show(); } else{ $(".info").html("No Checkbox checked. The modal will not show!"); $(".modal").hide(); } });
.info { border: 1px solid green; } .modal { margin-top: 5px; border: 1px solid blue; display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" id="check-all"> <input type="checkbox" class="check"> <input type="checkbox" class="check"> <input type="checkbox" class="check"> <button id="btn-edit" type="button" class="btn btn-primary"> EDIT </button> <div class="info"> Check the checkboxes! </div> <div class="modal"> I only show if ONLY one checkbox is checked. </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.