简体   繁体   English

使用Knockout中的data-bind不会检查复选框

[英]Checkbox will not stay checked with the data-bind in Knockout

I have a checkbox that I want to execute a function when it is clicked. 我有一个复选框,我想在单击时执行一个函数。 The issue is, it needs to execute a different function based on whether or not the checkbox is checked. 问题是,它需要根据是否选中复选框来执行不同的功能。 So, I setup a status observable that is set to false by default: 所以,我设置了一个默认情况下设置为false的状态observable:

self.status = ko.observable(false);

I binded a function to the checkbox on click: 我将一个函数绑定到单击复选框:

<input type="checkbox" data-bind="click: modify, checked: status">

And I created the function to execute a different function based on whether or not the status observable is true or false: 我根据状态observable是true还是false创建了执行不同函数的函数:

self.modify = function() {
    if (self.status() == false) {
        // Some other code
        alert('Now True!')
        self.status(true);
    }
    else if (self.status() == true) {
        // Some other code
        alert('Now False!');
        self.status(false)
    }
}

This function works perfectly fine. 这个功能非常好。 It alerts the correct message based on whether or not status is true or false, however the issue is, I cannot seem to get the checkbox to stay checked when the status is true. 它根据状态是真还是假来警告正确的消息,但问题是,当状态为真时,我似乎无法使复选框保持检查状态。

http://jsfiddle.net/tsnolan23/s441hggL/ http://jsfiddle.net/tsnolan23/s441hggL/

Any ideas on why the checkbox is not staying checked? 关于为什么复选框没有保持检查的任何想法?

Here's what's happening: 这是发生了什么:

  1. You click the checkbox. 单击复选框。
  2. The click handler fires, it sets status . 点击处理程序触发,它设置status
  3. The checked handler fires, it changes the checkbox status. 已检查的处理程序将触发,它会更改复选框状态。
  4. The event bubbles to the browser, which reverts the change. 事件会冒泡到浏览器,从而恢复更改。
  5. Knockout notices this, and updates status again. Knockout注意到这一点,并再次更新status

Here's a whole different approach, using writeable computeds for this purpose: 这是一个完全不同的方法,为此目的使用可写计算:

 var Vm = function() { var self = this; var _isActive = ko.observable(false); self.isActive = ko.computed({ read: _isActive, write: function(newValue) { _isActive(newValue); if (!!newValue) { alert("Something..."); } else { alert("Another thing."); } } }); }; ko.applyBindings(new Vm()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <label> <input type="checkbox" data-bind="checked: isActive" /> Check me! </label> 

If you must for some reason use the click binding, then you should do two things: 如果由于某种原因必须使用click绑定,那么你应该做两件事:

  • return true from your handler 从你的处理程序return true
  • set clickBubble: false on the bindings 在绑定上设置clickBubble: false

For example: 例如:

 var Vm = function() { var self = this; self.isActive = ko.observable(false); self.modify = function() { if (self.isActive() == false) { // Some other code alert('Now True!') } else if (self.isActive() == true) { // Some other code alert('Now False!'); } return true; } }; ko.applyBindings(new Vm()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <label> <input type="checkbox" data-bind="click: modify, clickBubble: false, checked: isActive" /> Check me! </label> <pre data-bind="text: ko.toJSON($root)"></pre> 

You don't need two bindings. 您不需要两个绑定。 The status will change when you check the box. 选中此复选框后,状态将会更改。 All you need is a subscription to the observable to notice that. 您只需订阅observable即可注意到。 (The writable computed solution is about the same, but a bit more complicated than necessary.) (可写的计算解决方案大致相同,但比必要的复杂一点。)

 var Vm = function() { var self = this; self.isActive = ko.observable(false); self.isActive.subscribe(function (newValue) { if (newValue) alert("Checked"); else alert("Unchecked"); }); }; ko.applyBindings(new Vm()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <label> <input type="checkbox" data-bind="checked: isActive" /> Check me! </label> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM