如何禁用 bootstrap 切换按钮
how to disable bootstrap toggle button
我正在尝试禁用 bootstrap 切换按钮,但还没有成功
<div class="toggle btn btn-default" disabled data-toggle="toggle" data-bind="toggle:isok(), css:{off:!isok()}">
<div class="toggle-group">
<label class="btn btn-primary toggle-on">ON</label>
<label class="btn btn-default active toggle-off">OFF</label>
<span class="toggle-handle btn btn-default"></span>
</div>
</div>
我正在使用knockout绑定数据。我硬编码添加禁用 属性,如您所见,但它显示为禁用,但我仍然可以打开或关闭它。
如何解决这个问题?
您将 disabled
属性硬编码到 div
元素。 div 无法禁用。将 div 更改为 button
它将起作用,请参见下面的示例,其中切换是手工制作的:
function Test() {
var _this = this;
_this.disabled = ko.observable();
_this.active = ko.observable();
_this.toggleActive = function() {
_this.active(!_this.active());
}
_this.toggleDisable = function() {
_this.disabled(!_this.disabled());
}
}
ko.applyBindings(new Test());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<button type="button" class="btn btn-primary" aria-pressed="false" autocomplete="off" data-bind="css: {active: active}, click: toggleActive, enable: disabled">
<span data-bind="visible: active">ON</span>
<span data-bind="visible: !active()">OFF</span>
</button>
<a href="#" data-bind="click: toggleDisable">toggle disable</a>
我正在尝试禁用 bootstrap 切换按钮,但还没有成功
<div class="toggle btn btn-default" disabled data-toggle="toggle" data-bind="toggle:isok(), css:{off:!isok()}">
<div class="toggle-group">
<label class="btn btn-primary toggle-on">ON</label>
<label class="btn btn-default active toggle-off">OFF</label>
<span class="toggle-handle btn btn-default"></span>
</div>
</div>
我正在使用knockout绑定数据。我硬编码添加禁用 属性,如您所见,但它显示为禁用,但我仍然可以打开或关闭它。
如何解决这个问题?
您将 disabled
属性硬编码到 div
元素。 div 无法禁用。将 div 更改为 button
它将起作用,请参见下面的示例,其中切换是手工制作的:
function Test() {
var _this = this;
_this.disabled = ko.observable();
_this.active = ko.observable();
_this.toggleActive = function() {
_this.active(!_this.active());
}
_this.toggleDisable = function() {
_this.disabled(!_this.disabled());
}
}
ko.applyBindings(new Test());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<button type="button" class="btn btn-primary" aria-pressed="false" autocomplete="off" data-bind="css: {active: active}, click: toggleActive, enable: disabled">
<span data-bind="visible: active">ON</span>
<span data-bind="visible: !active()">OFF</span>
</button>
<a href="#" data-bind="click: toggleDisable">toggle disable</a>