I am using knockout.js for data binding.Actually there is a form which some fields accepting name,number,email etc.Suppose if any one of the field is not filled and the save button is pressed then that button goes to disabled state.Till now its working fine.
Now if I fill the empty fields then I want to enable that button again and I dont know how to do.Can any body please help me how to do?
The following is the js code
self.newPatient = ko.asyncCommand({
execute: function (complete) {
var isValid = $('#addPatientForm').parsley('validate');
if (isValid) {
var patientJson = ko.toJSON(self.patient());
formdata.append("json", patientJson);
//self.enableButton(false);
var imagepath = $.ajax({
url: projectUrl + "newPatient",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
formdata = new FormData();
imagepath = res;
var length = self.patients().length;
var patient = self.patient();
// self.enableButton(true);
}
});
$('.alert-patient-success').show();
self.patients.removeAll();
self.getPatients();
/* $.when(self.patients.push(self.patient()),self.patient(new Patient()),self.dirtyFlag1().reset(),$('#patientTabs a:last').tab('show'))
.always(complete);*/
}
},
canExecute: function (isExecuting) {
return !isExecuting && isDirty() && isValid();
}
});
this is html code
<script id="patientMetadataTemplate" type="text/html">
<form id="addPatientForm" data-validate="parsley">
<div class="control-group">
<label class="control-label" for="inputIcon">Name :</label>
<div class="controls">
<div class="input-prepend" >
<span class="add-on"><i class="icon-hand-right"></i></span>
<input class="span8" type="text" data-bind="value: name" data-required="true" data-trigger="change" name="name">
</div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputIcon">Address :</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-hand-right"></i></span>
<input class="span8" name="address" type="text" data-bind="value: address" data-required="true" data-trigger="change">
</div>
</div>
</div>
I have a fiddle also for the above js and html.
something like this:
First you disable the button, on each form element change you check if it is filled out and then you remove the disabled attr. I haven't tested this, but it should get you started in the right direction.