Devuelve un valor al usar jQuery.cada()?
Quiero devolver false y volver de la función si encuentro el primer cuadro de texto en blanco
function validate(){
$('input[type=text]').each(function(){
if($(this).val() == "")
return false;
});
}
Y el código anterior no funciona para mí :( ¿alguien puede ayudar?
31
Author: Prashant Lakhlani, 2010-09-29
2 answers
Estás saltando, pero desde el bucle interior , en su lugar usaría un selector para tu verificación específica de "sin valor", como este:
function validate(){
if($('input[type=text][value=""]').length) return false;
}
O bien, establezca el resultado a medida que vaya dentro del bucle, y devuelva ese resultado del bucle exterior:
function validate() {
var valid = true;
$('input[type=text]').each(function(){
if($(this).val() == "") //or a more complex check here
return valid = false;
});
return valid;
}
56
Author: Nick Craver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-29 09:26:55
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-29 09:26:55
Puedes hacerlo así:
function validate(){
var rv = true;
$('input[type=text]').each(function(){
if($(this).val() == "") {
rv = false; // Set flag
return false; // Stop iterating
}
});
return rv;
}
Eso supone que quieres devolver true
si no lo encuentras.
Puede encontrar que esta es una de esas sitautions donde no desea usar each
en absoluto:
function validate(){
var inputs = $('input[type=text]');
var index;
while (index = inputs.length - 1; index >= 0; --index) {
if (inputs[index].value == "") { // Or $(inputs[index]).val() == "" if you prefer
return false;
}
}
// (Presumably return something here, though you weren't in your example)
}
14
Author: T.J. Crowder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-29 09:28:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-29 09:28:15