剔除已选中并单击两者(防止出现双重复选框切换) [英] Knockout checked and click both (prevent double checkbox switch)
问题描述
我有这样描述的复选框:
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()只是反转了可观察到的"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.
如果我将handle_compare绑定到按钮-一切正常,则复选框会正常切换.有没有办法允许这两个绑定?
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/
推荐答案
您需要使用内联单击处理程序以返回true:
You need your inline click handler to return true:
http://jsfiddle.net/g5rpcw2c/2/
两个:
<input id="checkbox1" type="checkbox" data-bind="
click: function () { handle_compare($element); return true; },
checked: is_product_compared">
或(由于handle_compare已经返回true):
or (since handle_compare already returns true):
<input id="checkbox1" type="checkbox" data-bind="
click: function () { return handle_compare($element) },
checked: is_product_compared">
这篇关于剔除已选中并单击两者(防止出现双重复选框切换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!