我遇到了 Vue 的问题,当元素失去焦点时,自定义输入上的 v-model 没有更新。
例如,如果我有两个这样的自定义输入元素,我在第一个输入一个值,然后快速制表到第二个,它会将第一个的值截断一个或两个字符。
这是自定义表单元素的 HTML 和在 keyup 上调用的方法:
<input type="string" ref="input"
v-on:keyup="checkInput"
v-on:focus="hasFocus"
v-on:focusout="lostFocus">
checkInput(event){
console.log("Checking input for " + this.title);
// If keycode is 9 (tab), ignore. Don't raise errors or emit values if user is just tabbing into field
if(event.keyCode !== 9){
var text = this.$refs.input.value;
this.check = this.regex.test(text);
this.$emit('input', text);
this.$emit('check', this.check);
console.log("Emit: " + text);
this.hasFocus();
}
},
这是它在模板中的初始化方式
<CustomFormElement:title="'Name'" :valid="'^(?!\\s*$).+'"
:errorMessage="'Name cannot be blank'"
v-model="data.name"
@check="(value) => formCheck('name', value)"
class="form_field"
></CustomFormElement>
当我去 checkInput 并打印出正在发出的文本时,如果我快速键入并跳到下一个输入,它会将值截断几个字符。例如,如果我输入“John Smith”,data.name 将等于“John Smi”。
v-on:keyup 在元素失去焦点后停止触发是否有任何原因。元素失去焦点后 v-model 没有更新的任何原因?