Mask-Money does not work on input array

Asked

Viewed 168 times

0

I have a form with fields in array

<tr>
    <td><input id="item" name="item[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->item }}"></td>
    <td><input id="descricao" name="descricao[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->descricao }}"></td>
    <td><input id="quantidade" name="quantidade[]" type="number" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->quantidade }}"></td>
    <td><input id="valor_unitario" name="valor_unitario[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->valor_unitario }}"></td>
    <td><input id="desconto" name="desconto[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->desconto }}"></td>
    <td><input id="total" name="total[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->quantidade * $servico->valor_unitario - $servico->desconto }}" readonly></td>
    <td><a onclick="RemoveTableRowOrcamento(this)" id="delete" class="delete"><i class="fa fa-trash"></i></a></td>
</tr>

and this script

$(function() {
    $("#valor_unitario").maskMoney();
    $("#desconto").maskMoney();
});

but Mask only works in the first two inputs.. value and discount, then it doesn’t work but in the next ones of the array.

  • 1

    Dear Micheal, Ids cannot repeat themselves, every TR you are probably repeating Ids

  • Michael, if you have already obtained the answer, close this question by stating the answer.

2 answers

2


The id is a unique identifier, so it will only be inserted in the first tag that it was found, try to use a class as follows:

<tr>
   <td><input id="item" name="item[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->item }}"></td>
   <td><input id="descricao" name="descricao[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->descricao }}"></td>
   <td><input id="quantidade" name="quantidade[]" type="number" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->quantidade }}"></td>
   <td><input id="valor_unitario" name="valor_unitario[]" type="text" class="form-control form-control-sm valor-unitario" aria-required="true" aria-invalid="false" value="{{ $servico->valor_unitario }}"></td>
   <td><input id="desconto" name="desconto[]" type="text" class="form-control form-control-sm desconto" aria-required="true" aria-invalid="false" value="{{ $servico->desconto }}"></td>
   <td><input id="total" name="total[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ $servico->quantidade * $servico->valor_unitario - $servico->desconto }}" readonly></td>
   <td><a onclick="RemoveTableRowOrcamento(this)" id="delete" class="delete"><i class="fa fa-trash"></i></a></td>
</tr>

And the script:

$(function() {
   $(".valor-unitario").maskMoney();
   $(".desconto").maskMoney();
});
  • exactly that brother vlw! you’re the guy//

0

There is no syntax error in your code, it will add the Mask in the Ids found in the inputs, in case the ones that were defined by you: #valor_unitario and #desconto

See the example below:

$(function() {
  $("#valor_unitario").maskMoney();
  $("#desconto").maskMoney();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>

<input id="desconto" name="desconto[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" >
<br/>
<input id="valor_unitario" name="valor_unitario[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" >

Browser other questions tagged

You are not signed in. Login or sign up in order to post.