开发者

knockoutjs and jQuery mobile - checkbox doesnt update value

开发者 https://www.devze.com 2023-04-05 15:00 出处:网络
When binding a checkbox to the checked binding and using jQuery mobile the observable does not get updated when the enhanced checkbox is checked.

When binding a checkbox to the checked binding and using jQuery mobile the observable does not get updated when the enhanced checkbox is checked.

see here

I have looked at the code for both libraries and it seems that knockoutjs is bound to the click event, yet jQuery mobile only updates the开发者_如何学编程 checked value which does not fire this event, i have fudged around with hacks to try to get this working with no joy..

Does anyone have a workaround for this ?


There might be a better way to do this, but have a look at this:

http://jsfiddle.net/vQRWQ/

I've added a new bindingHandler:

ko.bindingHandlers.checkbox = {
    update: function(element, valueAccessor) {

        var value = valueAccessor();

        var valueUnwrapped = ko.utils.unwrapObservable(value); 

        $(element).attr("checked", valueUnwrapped).checkboxradio("refresh");
    }
};

And used the "event" binding:

data-bind="event: {change: function(){ checked(!checked()); } }, checkbox: checked"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号