【发布时间】:2021-07-16 02:22:31
【问题描述】:
我想创建一个用户可以填写的输入字段。问题是我不希望他们用特殊字符填写这个字段。目前,我有这个 html 设置:
<input class="rc-input-editing" id="bioInput" type="text" v-model="wrappedBioName">
还有这个 Vue.js 设置(如您所见,我正在尝试使用计算的 setter 来解决这个问题):
data () {
return {
newBioName: '',
}
},
computed: {
wrappedBioName: {
get () {
alert('getting new name!')
return this.newBioName
},
set: function (newValue) {
const restrictedChars = new RegExp('[.*\\W.*]')
if (!restrictedChars.test(newValue)) {
this.newBioName = newValue
}
}
}
目前,我的问题是客户端能够继续填写文本输入字段,即使 this.newBioName 没有更新。换句话说,他们可以在输入字段中输入特殊字符,即使 this.newBioName 没有被这些特殊字符更新。
鉴于我目前对 v-model 的理解,这种行为与我的预期不同。根据我到目前为止所阅读的内容,v-model 将输入元素绑定到一些 vue 实例数据,并将 vue 实例数据绑定到输入元素(双向绑定)。因此,我期望文本输入字段中的文本将直接匹配 this.newBioName 的值。
很明显,我错过了一些东西,希望能有第二双眼睛!
【问题讨论】: