Issue
As you can see in my included JSFiddle here, I am trying to enable a Toggle Button after ticking all other Toggle/switches on, it is definitely working with a Simple Submit Button show there, but not so much with a Toggle that says Pending/Done (Pending(Orange color), (Done(Green color). Any Help is appreciated.
$(document).on("change keyup", ".required", function(e) {
let Disabled = $(".required").length;
$(".required").each(function() {
if (this.checked) Disabled--;
});
if (Disabled) {
$(".toggle-disabled").prop("disabled", true);
} else {
$(".toggle-disabled").prop("disabled", false);
}
});
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap5-toggle@5.0.6/css/bootstrap5-toggle.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap5-toggle@5.0.6/js/bootstrap5-toggle.ecmas.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
</head>
<div class="container">
<form action="#">
<div class="container">
<div class="form-check form-switch">
<input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Gmail</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Pika</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Justice</label>
</div>
<!--A Simple Submit Works Fine -->
<div class="form-check form-switch">
<button type="button" class="toggle-disabled" disabled>Submit</button>
</div>
</div><br>
<!--Toggle button doesn't work -->
<input type="checkbox" class="toggle-disabled" data-toggle="toggle" data-size="small" data-onlabel="Done" data-offlabel="Pending" data-onstyle="success" data-offstyle="warning" disabled>
</form>
</div>
Solution
I've fixed @chris-barr edit to show your orange switch. Sorry but I don't have reputation to post comment.
Your problem is how you dynamically enable/disable the .toggle-disabled
Here is how
$(document).on("change keyup", ".required", function(e) {
let Disabled = $(".required").length;
$(".required").each(function() {
if (this.checked) Disabled--;
});
if (Disabled) {
$(".toggle-disabled").prop("disabled", true);
$(".toggle-disabled")["1"].bootstrapToggle('disable');
} else {
$(".toggle-disabled").prop("disabled", false);
$(".toggle-disabled")["1"].bootstrapToggle('enable');
}
});
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap5-toggle@5.0.6/css/bootstrap5-toggle.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap5-toggle@5.0.6/js/bootstrap5-toggle.ecmas.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
</head>
<div class="container">
<form action="#">
<div class="container">
<div class="form-check form-switch">
<input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Gmail</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Pika</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Justice</label>
</div>
<!--A Simple Submit Works Fine -->
<div class="form-check form-switch">
<button type="button" class="toggle-disabled" disabled>Submit</button>
</div>
</div><br>
<!--Toggle button doesn't work -->
<input type="checkbox" class="toggle-disabled" data-toggle="toggle" data-size="small" data-onlabel="Done" data-offlabel="Pending" data-onstyle="success" data-offstyle="warning" disabled>
</form>
</div>
You need to use these to actually enable/disable them
// To disable
$(".toggle-disabled")["1"].bootstrapToggle('disable');
// To enable
$(".toggle-disabled")["1"].bootstrapToggle('disable');
Because Bootstrap Toggle
will insert their actual component next to your input. So modify your input
disable property won't modify Bootstrap Toggle
Answered By - duckstery
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.