Knockout.Js doesn't detect programmatically ch

2019-04-21 08:26发布

I have a simple checkbox which has its value adjusted by a bit of infrastructure that I'm writing to persist state in localStorage. I am aware that this would normally be done by setting the viewModel but the infrastructure is unaware of any knockout bindings and has no access to them.

I didn't think this would be a problem as I assumed that knockout was running off the 'change' event, however $checkbox.prop('checked', true).trigger('checked') fails to trigger my observable. Even using the click event directly doesn't get me quite what I need.

What events does knockout wire to? How do I get it to read the state of the control?

jsbin showing this weird behavior here. Try selecting the checkbox directly versus clicking the button.

7条回答
2楼-- · 2019-04-21 08:59

I found a better way of doing that. Just put this code inside of your domready event:

    $("input:checkbox").bind('change', function () {
        $(this).triggerHandler('click');
    });
查看更多
登录 后发表回答