Knockout checked and click both (prevent double checkbox switch)

倾然丶 夕夏残阳落幕 提交于 2019-12-24 04:21:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!