vue監(jiān)聽input標(biāo)簽的value值方法
由于項(xiàng)目需要做實(shí)時(shí)搜查詢數(shù)據(jù),所以需要監(jiān)聽input標(biāo)簽的value,這里使用的前端框架vue
<input id="materialSearch" type="text" @keyup.enter="search" @input="search($event)"/>
這里的重點(diǎn)是:@input="search($event)",表示當(dāng)文本框有內(nèi)容輸入時(shí),則調(diào)用search方法
/*模糊搜索*/
search: function (event) {
//方法一:直接通過event.data可以獲得文本內(nèi)容
if(event.data!=null){
this.materialName = event.data;
}
//方法二:獲取DOM對(duì)象取value值
this.materialName = event.currentTarget.value;
//方法三:通過js獲取
this.materialName = document.getElementById("materialSearch").value;
}
拓展知識(shí):Vue 監(jiān)聽多個(gè)input框是否都存在值的方法
如下所示:
<div class="inner clear"> <input type="text" placeholder="第一個(gè)輸入框" v-model="input1"> </div> <div class="inner clear"> <input type="text" placeholder="第二個(gè)輸入框" v-model="input2"> </div> <div class="inner clear"> <input type="text" placeholder="第三個(gè)輸入框" v-model="input3"> </div>

script部分:
export default {
data:function(){
return {
input1:'',
input2:'',
input3:'',
ifExist:'',
}
},
}
在頁面中插入一個(gè)隱藏域:
<div style="display:none" >{{ exitsVal }}</div>
利用Vue的computed屬性
computed:{
exitsVal:function(){
this.ifExist=Number(Boolean(this.userName))+Number(Boolean(this.mailCode))+Number(Boolean(this.mailAd));
}
},
用watch監(jiān)聽data中 ifExist的值
watch:{
ifExist(newVal,oldVal){
if(Number(newVal) === 3){
三個(gè)input框內(nèi)都有值時(shí)需要做的操作
}else{
至少一個(gè)沒有值
}
}
}
以上這篇vue監(jiān)聽input標(biāo)簽的value值方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

