I have the following html form:
<form id="searchForm" action="/pages/index" accept-charset="UTF-8" method="get">
<input id="toValidate" type="text" name="search_form[mixed]"></div>
<input id="toValidate" type="text" name="search_form[first_name]"></div>
<select name="search_form[state]">
<option selected="selected" value="All">All</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="WY">Wyoming</option>
</select>
<input type="submit" name="commit" value="SEARCH" id="submit" data-disable-with="SEARCH">
</form>
I want to make the user enter at least one input field. They way I thought to do that is with the following Javascript code:
function checkFields(form) {
var inputs = form.find('toValidate').not('[type="submit"],[type="button"],[type="reset"]');
var filled = inputs.filter(function(){
return $.trim($(this).val()).length > 0;
});
if(filled.length === 0) {
return false;
}
return true;
}
$(function(){
$('#searchForm').on('submit',function(e){
var oneFilled = checkFields($(this));
if (oneFilled) {
alert('At least ONE field filled!');
} else {
e.preventDefault();
if (confirm('NO FIELDS FILLED OUT!')){
$('#submit').disabled = false;
}else{
$('#submit').disabled = false;
}
}
});
});
The preventDefault() function disables the submit button but then I cannot re-enable the button again once I alerted the user. It's funny though, because when I paste the code $('#submit').disabled = false;
on the console it re-enables the button. What should I do to re-enable the button within the JS code then?
From the eyes of a user:
When preventDefault(); it's called the button is disabled and then, once the user accepts the confirmation window she cannot submit the form again because the button is disabled. How can I make it so that the button becomes enabled once again after the confirmation window is accepted and the user can click on the 'SEARCH' button.
Thank you.