我正在Vue中构建一个组件.js.我在页面上有一个输入,用户可以在其中请求一定的信用额度.目前,我正在try 创建一个函数,在输入时将输入量记录到控制台.(最终,我将根据用户输入显示/隐藏请求的文档.我不希望他们必须单击提交按钮.)

下面的代码在我点击输入字段时记录.这是我的部件.vue:

<template>
    <div class="col s12 m4">
      <div class="card large">
        <div class="card-content">
          <span class="card-title">Credit Limit Request</span>
          <form action="">
            <div class="input-field">
              <input v-model="creditLimit" v-on:change="logCreditLimit" id="credit-limit-input" type="text">
              <label for="credit-limit-input">Credit Limit Amount</label>
            </div>
          </form>
          <p>1. If requesting $50,000 or more, please attach Current Balance Sheet (less than 1 yr old).</p>
          <p>2. If requesting $250,000 or more, also attach Current Income Statement and Latest Income Tax Return.</p>
        </div>
      </div>    
    </div>
  </div>
</template>

<script>
export default {
  name: 'licenserow',
  data: () => ({
    creditLimit: ""
  }),
  methods: {
    logCreditLimit: function (){
      console.log(this.creditLimit)
    }
  }
}
</script>

如果我将methods改为computed,它会工作——但每次记录值时,我都会得到一个错误,说Invalid handler for event: change.

推荐答案

使用input事件.

<input v-model="creditLimit" v-on:input="logCreditLimit" id="credit-limit-input" type="text">

change only fires when the element loses focus表示输入元素.input在每次文本更改时激发.

Vue.js相关问答推荐

如何使用composition api v-model Select 框获取其他JSON 值?

如何在 Vuetify 2 中的轮播项目上放置 href 属性?

NUXT 3 在客户端获取数据

JSDoc 单文件 Vue 组件

从数据库中删除后,Vue.js $remove 不删除元素

是否可以让 html-webpack-plugin 从 css 生成