Knockout binding binding not working on trigger event

The checkbox data binding value does not change when I try to trigger a click event.

I created jsfiddle, when I click the button, I expect the value binding to change, but no.

http://jsfiddle.net/2T9QZ/13/

Any help?

+5
source share
1 answer

Calling a trigger ("click") in jQuery simply fires the click event handler. In fact, this does not cause a click (and, thus, a change in the checked state) in this flag - the only time it does something like this, in the case when the element has the function property in the same way as an event (for example, form.submit()- but not there checkbox.click()).

, :

var viewModel = {
    IsSelected: ko.observable(false) // Initially false
};

ko.applyBindings(viewModel);


$('#buttonInput').click(function(){
    viewModel.IsSelected(true); // <-------
    // Or, in order to toggle:
    // viewModel.IsSelected(!viewModel.IsSelected());
});

. , . checkbox checked - , IsSelected, IsSelected checked .

+4

All Articles