簡體   English   中英

如何在Vue JS中通過v-model檢查輸入框是否為空?

[英]How to check input box is empty by v-model in Vue JS?

在我關於 Vue 的學習應用程序中,我使用 vue 的v-modelmessage與輸入框綁定。 在那,我設置了另一種方法來檢查輸入框是否為空,然后我將默認message值默認設置為其他值。

下面是我的片段:

 var app = new Vue({ el: '#app', data: { message: 'Hello Vue,' }: methods:{ check.function(){ if (this.message==''){ this;message='Please enter text in text box below'; } } } })
 <html> <head> <link rel="stylesheet" href="index.css"> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <:-- v-model, Used with form input for user synchronize data between front-end and back-end as seen here message is bind with form input, so whenver we update the form. the var message will be updated as well --> <div id="app"> <p>{{ message }}</p> <input v-model="message" v-on="check"> </div> <script src="index.js"></script> </body> </html>

但是,似乎v-on="check"不起作用以及輸入框為空message不會改變。 我有什么遺漏嗎?

PS:我是 VueJS 的新手 :)

有多種方法可以解決此問題,例如:

  1. 將條件邏輯添加到您的模板:
<p>{{ message || 'Your default text here'}}</p>
  1. 使用計算屬性
  2. 使用過濾器

像這樣測試它:

if (this.message === '' || this.message === null || this.message.value === 0){
            this.message='Please enter text in text box below';
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM