【问题标题】:What's the difference between @input and @change in Vue <input type="file">?Vue <input type="file"> 中的@input 和@change 有什么区别?
【发布时间】:2020-11-30 06:34:49
【问题描述】:

请看这两个代码sn-ps,它们都输出相同的结果:

<template>
  <input type="file" @input="input" />
</template>

<script>
export default {
  methods: {
    input(event) {
      console.log(event.target.files);
    },
  },
};
</script>
<template>
  <input type="file" @change="change" />
</template>

<script>
export default {
  methods: {
    change(event) {
      console.log(event.target.files);
    },
  },
};
</script>

&lt;input type="file" /&gt;元素中的@input@change事件有什么区别吗?

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    这些事件继承自javascript事件oninputonchange,根据this你可以看到区别:

    oninput 事件在元素获得用户输入时发生。 当 &lt;input&gt;&lt;textarea&gt; 元素的值发生更改时,将发生此事件。 提示:此事件类似于onchange 事件。不同之处在于oninput 事件在元素的值发生更改后立即发生,而onchange 事件发生在元素失去焦点时,在内容发生更改后。另一个区别是onchange 事件也适用于&lt;select&gt; 元素。

    简单输入文本示例:

    Vue.config.devtools = false;
    Vue.config.productionTip = false;
    
    new Vue({
      el: '#app',
      methods: {
        input() {
          console.log("input")
        },
        change() {
          console.log("change")
        }
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
      <input @change="change" @input="input" />
    </div>

    【讨论】:

    • 按下 'enter' 将触发 onchange 以及当已经进行一些更改时。
    • 是的,如果输入被表单元素包裹,它可以提交表单
    猜你喜欢
    • 2013-12-07
    • 2010-11-26
    • 2015-11-14
    • 2017-12-04
    • 2020-10-02
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多