I have a checkbox and click event for checkbox for updating data. When I click on the checkbox the data is updating but the checkbox does not get not checked.
This is my html code:
<td>
<input type="checkbox" data-bind="checked: status, disable: status, click: $root.UpdateStatus" />
</td>
This is my script:
self.UpdateStatus = function (tblUsers) {
$.ajax({
type: "POST",
url: 'SinglePageApp.aspx/UpdateStatus',
data: "{statusVal: 'true',goalId: " + tblUsers.goalId + "}",
contentType: "application/json; charset=utf-8",
success: function (result) {
alert(result.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
}
});
};
I want my checkbox to get checked when it is clicked. And after that put updated data after checkbox clicked.
It seems like you're disabling the checkbox if status = false, but the only way to set status to true is if you check it. That's why the checkbox never gets checked even though knockout knows it's clicked.
Since it's disabled, you won't be able to check the checkbox, so you have to remove the
disable: status
data binding, or place thedisable
logic some place so you can actually change the value of the checkbox.I tried it here and it works when
UpdateStatus
returnstrue
(not preventing the value-change) andstatus
must obviously be an observable value.See: http://knockoutjs.com/documentation/click-binding.html
Edit: added example showing where to return
true
in the function. It has to be return from the actual function itself, not the Ajaxsuccess
orerror
handler.That's because the default behavior of event handlers in Knockout is to disable the default action for the event. In this case, your
click
handler does that.A solution that I recommend is to avoid using the
click
handler and have yourUpdateStatus
run as a subscription to status. In your view model constructor, you should addself.status.subscribe(self.UpdateStatus, self);
Another solution is the one mentioned here that involves returning
true
from yourclick
handler.