问题
I have the checkbox described like this:
<input id="checkbox1" type="checkbox" data-bind="
click: function () { handle_compare($element) },
checked: is_product_compared">
.handle_compare() just inverts the observable "is_product_compared", the problem is to allow a normal behavior to this checkbox, if I click on it, seems it double switches, and I never see the changes.
If I bind handle_compare to button - all is ok, checkbox switches normally. Is there a way to allow both this bindings?
You can see a demo here, button is ok, but the checkbox has wrong behavior.
http://jsfiddle.net/g5rpcw2c/1/
回答1:
You need your inline click handler to return true:
http://jsfiddle.net/g5rpcw2c/2/
either:
<input id="checkbox1" type="checkbox" data-bind="
click: function () { handle_compare($element); return true; },
checked: is_product_compared">
or (since handle_compare already returns true):
<input id="checkbox1" type="checkbox" data-bind="
click: function () { return handle_compare($element) },
checked: is_product_compared">
来源:https://stackoverflow.com/questions/29393665/knockout-checked-and-click-both-prevent-double-checkbox-switch