Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using v-on:input to change input's v-bind:value doesn't always work as expected #10716

Closed
nicolas-t opened this issue Oct 16, 2019 · 2 comments
Closed

Comments

@nicolas-t
Copy link

Version

2.6.10

Reproduction link

https://codesandbox.io/s/vue-template-8sl0s?fontsize=14

Steps to reproduce

  • Type letters in the first input

What is expected?

State's numericValue should be equal to DOM's input.value

What is actually happening?

State's numericValue and DOM's input.value are different.


I am trying to make an input that only accepts numbers as it's value.

In the codesandbox you'll see another exemple with "uppercase" that has the expecting behaviour.

I've openned a topic on forum.vuejs.org with no success :
https://forum.vuejs.org/t/filter-out-letters-from-an-input-to-only-keep-numbers/76205

@posva
Copy link
Member

posva commented Oct 16, 2019

This is working as expected: by filtering out letters before setting myNumericValue, this variable never changes and never triggers a re render, so the value on the input is preserved.

One way to get around this is to force update with this.$forceUpdate() after changing the value. A better way is to reset the input's value only:

const val = $event.target.value.replace(/[^\d]/g, ""); // strips letters, keeps numbers
      if (val === this.numericValue) $event.target.value = val
      else this.numericValue = val

@posva posva closed this as completed Oct 16, 2019
@nicolas-t
Copy link
Author

Oh yes !
It was obvious :)

thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants