This question has been done to death on SO and I'm really, really sorry! I've already taken the bones of the below idea from a couple of SO questions on the same theme.
All said though, I still can't get it to work as expected.
- It works OK if NONE are filled in.
- It works OK if the END input is filled in and not the others.
- It works OK if the MIDDLE input is filled in.
- If you fill in ONLY the FIRST input though, it alerts, but submits anyway?
$(document).ready(function (e) {
// completed count submit handler
$("#submit_counts_button").on('click', function () {
window.incomplete = false;
$('input[type=number]').each(function () {
if ($(this).val().length == 0) {
window.incomplete = true;
alert('Some fields are empty');
return false;
} else {
if (window.incomplete === false) {
$("#submit_counts_button").prop('disabled', true);
$("#submit_counts_button").html('Please Wait ...');
//$("#update_form").submit();
}
}
});
});
});
I'm sure it's something totally embarrassingly obvious but after a 16 hour day, I just can't see it. Any help appreciated ...
each()it iterates over the elements, so if an input without a value is encountered it goes to the first part in the condition, and thereturn falseis returning toeach()so the iteration stops. If the first element encountered has a value, it continues to the else part of the condition and submits the form, regardless of what the other inputs contain, so the entire concept is flawed.