I'm using the $ .each function of jQuery to run a JSON
, and given some validation I want to show a alert()
, leave the $.each
and the function that contains it, I know that return false;
leaves the function $.each
, but continues with the execution of the function.
Example
function guardarCuestionario() {
var preguntas = JSON.parse('cadena de texto con formato JSON');
$.each(preguntas, function () {
value = $("input:radio[name ='" + this.ID + "']:checked").val();
if (this.EsObligatoria && value === undefined) {
alert('Necesitas seleccionar una respuesta para todas las preguntas que no son opcionales');
return false; // solamente se sale del $.each pero no de la funcion
}
/*
Mas código
*/
});
console.log('No quiero que continúe hasta aquí si (this.EsObligatoria && value === undefined) se cumple!!!');
/*
Mas código
*/
}
I know that I can achieve what I want by assigning a value to a flag to know if at some point I enter the condition and check it at the end, but I want to know if there is any way out of the $.each
and the function that the contains in a sentence.