Set value inputmask jquery

Asked

Viewed 793 times

0

I am building a small system in jquery and in the editing part where I need to inform a value programmatically to the field Jquery Inputmask is not correct. Below is the example of my code and what happened on the screen.

$(".money")
        .inputmask('numeric',{"autoUnmask": true,
            radixPoint:",",
            groupSeparator: ".",
            allowMinus: false,
            prefix: 'R$ ',            
            digits: 2,
            digitsOptional: false,
            rightAlign: true,
            unmaskAsNumber: true
        });

<div class="form-group">
     <label>Valor:</label>
     <input type="text" value="0" id="value" class="form-control money watch" required>
</div>

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Edit 1 As requested in the comments as I am doing to put the value in the field. PS This Trigger I got from a comment on github stating that it worked, already tried with input too and even then it didn’t work.

  • @Sam ready! Thanks for responding!

  • Sorry buddy, but you probably didn’t understand, but that’s okay. In the pictures, I put the arrows and the lines to help. I don’t see how to further clarify what is happening. I have the field with id VALUE that is being configured with inputmask. However, when I enter the value of the field with val() it ignores the . of the value as shown in the first image.

  • I’m not new here, I know how the community works and that you should explain your question to the maximum, I researched a lot before coming to ask for people like you. I have no problem editing the question or answering more things, and as you can see the picture is 150.6 and in the field it is 1506 completely ignoring the .

  • No young man, I believe you are experienced and can help me, but the val() is in the second image. The second method is val() and just after Trigger which was indicated in this publication of github https://github.com/igorescobar/jQuery-Mask-Plugin/issues/261#issuecomment-71181680. And as had said the value appears wrong in the third picture, so I call the val to inform you.

1 answer

2


I understand your problem, because when you send a point value, example: 150.6 consider R$ 1506,00 and should be: R$ 150,60, then it has to set correctly as follows, changing the point by comma in the return, so:

value.val(object.value.replace('.',',')); 

that will work, example:

$(".money").inputmask('numeric',
{   autoUnmask: true,
    radixPoint:",",
    groupSeparator: ".",
    allowMinus: true,
    prefix: 'R$ ',            
    digits: 2,
    digitsOptional: false,
    rightAlign: true,
    unmaskAsNumber: true
});
$("#BtnSetaValor").click(function(){
  $(".money").val('150.6'.replace('.',','));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/jquery.inputmask.bundle.min.js"></script>

<div class="form-group">
     <label>Valor:</label>
     <input type="text" value="0" id="value" class="form-control money watch" required>
</div>
<br />
<div>
  <input type="button" value="Seta Valor" id="BtnSetaValor" />
</div>

Maybe it’s a limitation or even a limitation bug and what is good to do, open an Issue and report the problem to the community trying to solve.

  • 1

    It worked perfectly! Thanks. I don’t know if it’s a bug, maybe it’s a small limitation as to the configuration that was initially passed. But thank you young!

  • @Márcioeric is good to report in git, if you want of course ... but, that good worked

Browser other questions tagged

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