uniapp清空输入框的值
时间: 2023-08-07 10:02:06 浏览: 614
在 uni-app 中清空输入框的值,可以通过以下两种方式实现:
1. 通过双向绑定清空
在模板中,可以使用 `v-model` 将输入框的值与组件实例中的变量进行双向绑定,当需要清空输入框时,只需要将该变量值设置为空字符串即可。
```html
<template>
<input type="text" v-model="inputValue" />
<button @click="clearInput">清空</button>
</template>
<script>
export default {
data() {
return {
inputValue: ''
}
},
methods: {
clearInput() {
this.inputValue = ''
}
}
}
</script>
```
2. 通过组件引用清空
在组件中,可以通过 `$refs` 引用输入框组件,并调用其 `setValue` 方法将输入框值设置为空字符串。
```html
<template>
<input type="text" ref="input" />
<button @click="clearInput">清空</button>
</template>
<script>
export default {
methods: {
clearInput() {
this.$refs.input.setValue('')
}
}
}
</script>
```
需要注意的是,第二种方法需要在输入框组件中定义 `setValue` 方法,调用该方法时才能清空输入框的值。
阅读全文
相关推荐


















