I'm working on a calculation form and would like to disable the submit button until all of the fields have been filled. The form consists of one number input and four select options.
Though I've investigated a few different solutions, Like the following:
Disabling submit button until all fields have values
Enable submit button only when all fields are filled
For some reason, whenever I type a value into the number input, it immediately overrides the disabling of the submit button, whereas all of the select options should also be chosen before the submit button is enabled.
I've based my code on the second example linked above.
// Check if all fields have been filled out
// Initially disable submit button
$('#submit').attr('disabled', 'disabled');
//Check number input for value and compare to select options bind to input keyup, mouseup event
$("#acre").on('keyup mouseup', function() {
// check if input has value, search all select options, (id prefixed by #forest) if they have a value set.
if ($(this).val() != "" && $('[id^=forest]').val() != "" ){
$('#submit').removeAttr('disabled');
} else {
$('#submit').attr('disabled', 'disabled');
}
});
// Similar for select options
$('[id^=forest]').on('change', function() {
if ($(this).val() != "" && $('#acres').val() != "" ){
$('#submit').removeAttr('disabled');
} else {
$('#submit').attr('disabled', 'disabled');
}
});
Any advice would be appreciated.
Thanks,
Tak