3
Good morning person,
I’m having trouble coming up with a code.
I have several inputs that are created dynamically. I need to take the ID of the specific element to change its value.
EX: I have the following fields: Days, Absences, Vacancy, Leave, Sick Leave and Work Accident. The Days field has a value of 30, the other fields may vary depending on the employee. The sum of all fields(Absences, Vacancy, License, Medical License and Work Accident) can not exceed 30, moreover, as soon as the user enters something in these fields he has to immediately update the field Days reducing its value.
Follow the code below for better understanding.
@foreach ($servidores as $i => $servidor)
<form id="formid{{$i++}}" role="form" method="get" action="/mapa_servidor/editMapaServidor/{{$servidor->id}}">
<tr data-index="0" id="formulario{{$i}}" class="input-sm" {{$servidor->st_lancado == 'X'? $cor = "#38A8A2": $cor = "FFFFFF"}} style="background-color: {{$cor}}">
<td class="text-left">
<input type="text" class="nm_servidor" id="nm_servidor{{$i}}" name="nm_servidor[]" value="{{$servidor->nm_servidor}}" readonly>
<input type="hidden" id="dt_referencia" name="dt_referencia" value="{{$servidor->dt_referencia}}" readonly>
<input type="hidden" name="_token" value="{{csrf_token()}}" />
<br>
</td>
<td class="text-left">
<input type="text" class="nr_matricula" id="nr_matricula{{$i}}" name="nr_matricula[]" value="{{$servidor->nr_matricula}}" readonly>
</td>
<td class="text-left">
<input type="text" class="cd_departamento" id="cd_departamento{{$i}}" name="cd_departamento[]" value="{{$servidor->cd_departamento}}" readonly>
</td>
<td class="text-left" style="">
<input type="text" class="total nr_dias" id="nr_dias{{$i}}" name="nr_dias[]" value="{{rtrim($servidor->nr_dias)}}" readonly>
</td>
<td class="text-left" style="">
<input type="text" class="calc nr_faltas" id="nr_faltas{{$i}}" name="nr_faltas[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_faltas)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="calc nr_vacancia" id="nr_vacancia{{$i}}" name="nr_vacancia[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_vacancia)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="calc nr_licenca" id="nr_licenca{{$i}}" name="nr_licenca[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_licenca)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="calc nr_licenca_medica" id="nr_licenca_medica{{$i}}" name="nr_licenca_medica[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_licenca_medica)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="calc nr_acidente_trabalho" id="nr_acidente_trabalho{{$i}}" name="nr_acidente_trabalho[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_acidente_trabalho)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="nr_adicional_noturno" id="nr_adicional_noturno{{$i}}" name="nr_adicional_noturno[]" value="{{rtrim($servidor->nr_adicional_noturno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="nr_he_norm_diurno" id="nr_he_norm_diurno{{$i}}" name="nr_he_norm_diurno[]" value="{{rtrim($servidor->nr_he_norm_diurno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="nr_he_norm_noturno" id="nr_he_norm_noturno{{$i}}" name="nr_he_norm_noturno[]" value="{{rtrim($servidor->nr_he_norm_noturno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="nr_he_dom_diurno" id="nr_he_dom_diurno{{$i}}" name="nr_he_dom_diurno[]" value="{{rtrim($servidor->nr_he_dom_diurno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="nr_he_dom_noturno" id="nr_he_dom_noturno{{$i}}" name="nr_he_dom_noturno[]" value="{{rtrim($servidor->nr_he_dom_noturno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="checkbox" class="st_grat_reuniao" id="st_grat_reuniao{{$i}}" name="st_grat_reuniao[]" {{($servidor->st_grat_reuniao) == 'X'? 'checked': '' }} {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="text" class="ds_observacao" id="ds_observacao{{$i}}" name="ds_observacao[]" value="{{rtrim($servidor->ds_observacao)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
</td>
<td class="text-left" style="">
<input type="checkbox" class="st_lancado" id="st_lancado{{$i}}" name="st_lancado[]" {{$servidor->st_lancado == 'X'? 'checked': '' }} required>
</td>
<td class="text-left" style="">
<button type="submit" class="btn btn-default btn-xs">Salvar</button>
</td>
</tr>
</form>
@endforeach
The way I did, it is modifying all the inputs of name nr_dias. Instead of modifying only the line I need. I know I’m doing it wrong, but I can’t think of a solution.
$(document).on("keyup", ".calc", function() {
var sum = 0;
$(".calc").each(function(){
sum += +$(this).val();
});
if(sum > 30){
$(".total").val(30);
$(".calc").val('');
}else{
$(".total").val(30-sum);
}
});
I think the title of the question does not match the scope of the question...
– Lollipop
The problem has to be in the question and you know that the problem is not in how to get the element id because you already know how to do it.
– Lollipop
Another mistake is that you put in the title: javascript, but put tag jquery, leaves confused about who will answer... Should I answer in javascript or jquery?
– Lollipop
I am saying this because it will help you a lot to get something here. I also went through this, don’t be discouraged!
– Lollipop
Lollipop no problem. I will take more care next time. I changed the title of the question and removed the javascript tag.
– douglas pjuizfora
I see no problem in leaving the tag
javascript
provided that it alsojquery
. You can solve only with the logic ofjavascript
with nothing tojquery
, the problem could be aif
for example ;-)– Ricardo Pontual