I've read the following post about how to exit from a javascript function:
How can I exit from a javascript function?
I'm trying to adapt the example to my code, but I'm missing something / doing something wrong.
I have the following jquery code:
$('#main_submit_button').live('click', function(){
check_for_duplicate_rule().done(function(result) {
console.log("result from check for duplicate" + result);
if (result == 'true') {
$('#validation_error').html("A similar rule already exists");
return;
}
});
//}
console.log("i made it this far");
return false;
console.log($('#rule_form').serialize() );
}
When I test using duplicate data, the system correctly displays an error that says "A similar rule already exists" but it also displays the message "i made it this far" in the console. It DOES NOT display the results of the form serialize() command. I was expecting the code to just exit the .click even handler after setting the error message.
I've tried change to change the "return" statement to "return false" but that doesn't work either. Can you tell me what I'm doing wrong? Ultimately, what I'd like this routine to do is perform several client side validation checks before i submit the data to the server. So I will have multiple points in the function where I need to "return" or exit.
Thanks
EDIT 1
I've changed the code to look like:
var bduplicate = false;
check_for_duplicate_rule().done(function(result) {
console.log("result from check for duplicate" + result);
if (result == 'true') {
$('#validation_error').html("A similar rule already exists");
bduplicate = true
return;
}
});
//}
if (bduplicate) {
return;
}
console.log("i made it this far");
But it still prints the message "i made it this far". I think the problem might be with my the check_for_duplicate_rule function. ?? It looks like this:
function check_for_duplicate_rule() {
var parameters = {
num: $('#num').val(),
condition: $('#condition').val(),
cdidnumber: $('#cdidnumber').val()
}
return $.getJSON(
url = aURLdefinedsomewhere,
parameters,
function(data) {
//if (data=='true') {
//$('#validation_error').html("A similar rule already exists");
//}
}//end data
);//end getJSON
}
What I'm seeing in the console is that the "result from check for duplicate true" message is being printed AFTER the "i made it this far", which seems to indicate that I'm checking for results that I don't have... ? If this is true, i don't really know how to go about fixing it.