In following code $('body').delegate('.submit',... passed = search && passed; ... after click on button in form i have in var passed output as true in case var result in function search is false because data is ‘0’. How can fix it?
function search(e) {
e.preventDefault();
$('input').live('keyup change', function () {
var result = true;
$.ajax({
type: 'POST',
dataType: 'json',
url: 'myUrl',
data: myData,
async: false,
cache: false,
success: function (data) {
//in the here data is 0
if (data == 0) {
alert('data is 0')
result = false;
} else {
alert('data is not 0')
}
}
})
alert(result) // Output this alert is 'false' because data is '0'
return result;
})
}
$('.myclass_1').live('click', search);
$('.myclass_2').live('keyup change', search);
$('body').delegate('.submit', 'submit', function () {
var passed = true;
//passed = required_selectbox() && passed;
//passed = required_rediuses() && passed;
passed = search && passed;
alert(passed); // This output is always 'true' !!!!!!!!!!!!!?
if (!passed) {
$('#loadingDiv, #overlay').hide();
return false;
}
});
Try changing this line:
To:
Edit
After careful consideration of what you seemed to want your code to do, I rewrote the logic of it to this:
Setting a global variable
searchResultand modifying that in thesearch()function means you don’t have to return anything from it. You can now checksearchResultright in the submit button’s click event. You’re attaching thesearch()function as the callback to .myclass_1‘s click event and .myclass_2‘s keyup and change events, so now thesearch()function will fire and properly setsearchResultbased on the ajax response.