我无法让启用绑定(bind)在 Knockout JS 中工作。将 enabled 属性设置为 false,按钮不会被禁用,我仍然可以点击它。
参见 fiddle
<a class="btn btn-xl btn-primary"
href="#"
role="button"
data-bind="enable: enabled, click: clicked, visible: isVisible">
<i class="icon-only icon-ok bigger-130"></i>
</a>
var ViewModel = function(){
var self = this;
self.enabled = ko.observable(false);
self.isVisible = ko.observable(true);
self.clicked = function(){
alert('You clicked the button');
};
};
$(function(){
var model = new ViewModel();
ko.applyBindings(model);
})
最佳答案
Enable绑定(bind)不适用于您想要的任何东西。
This is useful with form elements like input, select, and textarea It also works with buttons. Like in my example http://jsfiddle.net/5CbnH/1/
但它不适用于您的链接。您正在使用 twitter bootstrap,他们使用 css 类启用/禁用他们的“按钮”。所以你必须使用 css binding像这样:
data-bind="css: { yourClass: enabled }"
检查 Bootstrap 中哪个类负责显示您的“按钮”,并使用 css 绑定(bind)相应地修改您的代码。
关于javascript - knockout 启用绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22701074/