In the following function, I would like to return false if data = 0 or empty, else return true – and for one input field it works.
However if I have two inputs with class="myclass_1" and one has a value and other input doesn’t have a value it does not work as expected.
On click on the button in the form using $('body').delegate('.submit', 'click',... when I alert(searchResult) output is true even though one field was empty
var searchResult = false;
var search = function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'myURL',
data: myData,
async: false,
success: function(data) {
if (data == '0') {
searchResult = false;
} else {
searchResult = true;
}
}
});
};
$('.myclass_1').live('keyup change', search);
$('body').delegate('.submit', 'click', function(e) {
var passed = true;
alert(searchResult)// this output is true
if (!(searchResult && passed)) { // <-- NOTICE THIS
$('#loadingDiv, #overlay').hide();
return false;
}
});
Hoping I understand your question:
You didn’t determine if there is an empty input, searchResult will be true when the last ajax-call was successfull(didn’t return 0)
You may store the success of the ajax-calls as data-attribute for every single input, so you will be able to check if the last ajax-call of all input’s was successfull: