首页 > 编程 > JavaScript > 正文

vue监听input标签的value值方法

2019-11-19 13:08:45
字体:
来源:转载
供稿:网友

由于项目需要做实时搜查询数据,所以需要监听input标签的value,这里使用的前端框架vue

<input id="materialSearch" type="text" @keyup.enter="search" @input="search($event)"/>

这里的重点是:@input="search($event)",表示当文本框有内容输入时,则调用search方法

/*模糊搜索*/search: function (event) {  //方法一:直接通过event.data可以获得文本内容  if(event.data!=null){    this.materialName = event.data;      }  //方法二:获取DOM对象取value值  this.materialName = event.currentTarget.value;  //方法三:通过js获取  this.materialName = document.getElementById("materialSearch").value;}

拓展知识:Vue 监听多个input框是否都存在值的方法

如下所示:

<div class="inner clear"> <input type="text" placeholder="第一个输入框" v-model="input1"></div><div class="inner clear"> <input type="text" placeholder="第二个输入框" v-model="input2"></div><div class="inner clear"> <input type="text" placeholder="第三个输入框" v-model="input3"></div>

script部分:

export default {  data:function(){  return {   input1:'',   input2:'',   input3:'',   ifExist:'',  } },}

在页面中插入一个隐藏域:

<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监听data中 ifExist的值

watch:{ifExist(newVal,oldVal){   if(Number(newVal) === 3){    三个input框内都有值时需要做的操作   }else{    至少一个没有值   }  }}

以上这篇vue监听input标签的value值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表