I have an editorfor field that is a checkbox and when it is changed from false to true and the form is submitted i need to track that the checkbox was changed and is marked true. Also it has to be a javascript or jquery function.
<div class="editor-field">
@Html.EditorFor(model => model.IsPublic)
</div>
If i need to explain this better please tell me. I just cant think of how else to explain.Thanks
Hope following code will do it:
@Html.HiddenFor(model => model.IsPublicChanged) @*create special model field for handling change event*@
$().ready(function () {
//catch change event and assign value to hidden field
$("input[name=IsPublic]").on("change", function () {
$("input[name=IsPublicChanged]").val('true');
});
});
Or some different js-code if you want to see if value of checkbox was changed comparing to it's initial value:
$().ready(function () {
var trackValue = $("input[name=IsPublic]").prop("checked");
$("form").on("submit", function () {
var actualValue = $("input[name=IsPublic]").prop("checked");
if (actualValue != trackValue) {
$("input[name=IsPublicChanged]").val('true');
}
});
});