Vue.js 验证输入框不能为空:使用 v-model 和 v-if 指令
<p>在 Vue.js 中,可以使用 <code>v-model</code> 指令来绑定输入框的值,并且可以使用 <code>v-if</code> 指令来判断输入框的值是否为空。例如:</p>
<pre><code class="html"><template>
<div>
<input type="text" v-model="inputValue">
<button @click="submitForm">提交</button>
<span v-if="inputValue === ''">输入框不能为空</span>
</div>
</template>
<script>
export default {
data() {
return {
inputValue: ''
}
},
methods: {
submitForm() {
if (this.inputValue === '') {
// 输入框不能为空,做出相应处理
return
}
// 其他操作
}
}
}
</script></code></pre>
<p>在这个例子中,使用 <code>v-model</code> 绑定了一个名为 <code>inputValue</code> 的 data 属性,这个属性保存了输入框的值。当点击提交按钮时,会调用 <code>submitForm</code> 方法,如果输入框的值为空,就会显示一个提示信息。如果输入框的值不为空,则可以执行其他操作。</p>
原文地址: https://www.cveoy.top/t/topic/oojN 著作权归作者所有。请勿转载和采集!