0
I need to interrupt the execution of .each
when an option is not met, I am using return false;
, but the code after the block each
is still running. Follow the example of the code:
function Salvar(element){
//$(element).parent().parent().children("td:nth-child(1)").css({"color": "red", "border": "2px solid red"});
var par = $(element).parent().parent();
var date = par.children("td:nth-child(1)").children().val();
par.children().each(function(i){
if(par.children("td:nth-child("+i+")").children().val() == ''){
par.children("td:nth-child("+i+")").children().addClass('error').focus();
return false;
}else{
$('#tbl-metas tbody tr td input').removeClass('error');
}
});
var data = par.children("td:nth-child(1)").children().val();
var va = par.children("td:nth-child(2)").children().val();
var vb = par.children("td:nth-child(3)").children().val();
var vc = par.children("td:nth-child(4)").children().val();
console.log('data: '+data+' va: '+va+' vb: '+vb+' vc: '+vc);
}
.error{
color: red;
border: 2px solid red;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" id="tbl-metas">
<caption>Cadastro de metas</caption>
<thead>
<tr>
<th>Mês</th>
<th>Venda 1</th>
<th>Venda 2</th>
<th>Venda 3</th>
<th>Ajuste</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="date" name="month" value="<?= date('Y-m').'-01' ?>" style="width:100%"></td>
<td><input type="text" class="money" name="txtVendaA" id="txtVendaA" style="width:100%"></td>
<td><input type="text" class="money" name="txtVendaB" id="txtVendaB" style="width:100%"></td>
<td><input type="text" class="money" name="txtVendaC" id="txtVendaC" style="width:100%"></td>
<td><button class="btn btn-success btn-sm" id="btnSave" name="btnSave" onclick="Salvar(this)">Salvar</button></td>
</tr>
</tbody>
</table>
And why don’t you make one
for
normal which is much simpler?– Maniero
And performatic. :)
– Luiz Felipe