简体   繁体   English

Go 到 maxLength 的下一个输入字段?

[英]Go to next input field at maxLength?

I have a form in Vue that has some combined inputs for styling reasons for a phone number input.我在 Vue 中有一个表单,它有一些组合输入,用于电话号码输入的样式原因。

My problem is that the user has to hit tab in order to go to the next input field of the phone number.我的问题是用户必须点击标签才能将 go 转到电话号码的下一个输入字段。

Is there a way to check for the max length of the current and input and when that is met go to the next input?有没有办法检查电流和输入的最大长度以及何时满足 go 到下一个输入?

<div class="combined-input combined-input--phone" :class="{'error--input': phoneInvalid('patientInformation')}">
  <div class="open-parenthesis"></div>

  <input type="text" id="phoneArea" maxlength="3" @blur="$v.formData.patientInformation.phone.$touch()" v-model.trim="$v.formData.patientInformation.phone.area.$model">

  <div class="close-parenthesis"></div>

  <input type="text" id="phoneA" maxlength="3" @blur="$v.formData.patientInformation.phone.$touch()" v-model.trim="$v.formData.patientInformation.phone.a.$model">

  <div class="dash"></div>

  <input type="text" id="phoneB" maxlength="4" @blur="$v.formData.patientInformation.phone.$touch()" v-model.trim="$v.formData.patientInformation.phone.b.$model">
</div>

Pretty sure this is not recommended UX-wise but here is an example on how you can achieve it: https://codesandbox.io/s/move-to-next-input-on-condition-103b5?file=/src/App.vue可以肯定的是,不建议在 UX 方面这样做,但这里有一个关于如何实现它的示例: https://codesandbox.io/s/move-to-next-input-on-condition-103b5?file=/src/应用程序.vue

The main part of useful code is有用代码的主要部分是

focusNextOncePopulated(event, max) {
  if (event.target.value.length === max) {
    const nextElement = this.$refs?.[`input-${Number(event.target.dataset.index) +1}`]
    if (nextElement) nextElement.focus()
  }
},

Explanation: each time you input a char, you check if the length of the field reached the maximum you've set on a particular input.说明:每次输入字符时,检查字段的长度是否达到您在特定输入上设置的最大值。 If it is and there is a similar input as a sibling, you focus it.如果是,并且有与兄弟相似的输入,则将其聚焦。
Adding a debounce of a few milliseconds would be nice too, performance-wise.在性能方面,添加几毫秒的debounce也会很好。

EDIT: to prevent any issues of trying to find the correct next input in the DOM, we setup some refs on each of the inputs (recommended way in Vue to select some DOM elements).编辑:为了防止尝试在 DOM 中找到正确的下一个input的任何问题,我们在每个输入上设置了一些参考(在 Vue 中推荐的方式为 select 一些 DOM 元素)。 Then, when we have reached the max , we increment our current's input data-index by one, which enables us to go to the next input.然后,当我们达到max时,我们将当前的输入data-index加一,这使我们能够将 go 到下一个输入。

PS: ?. PS: ?. syntax is optional chaining , it prevents ugly code and allows to avoid an error if there is no next input once max is reached.语法是可选链接,它可以防止丑陋的代码,并允许在达到max后如果没有下一个input则避免错误。

PS: Not sure if there is a way to directly get the $refs of an element on it's @input event but if there is, I didn't found out how to do it. PS:不确定是否有办法直接在它的@input事件上获取元素的$refs ,但如果有,我不知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM