Can someone tell me what the difference between the change and input events is?

我使用jQuery添加它们:

$('input[type="text"]').on('change', function() {
    alert($(this).val());
})

它也适用于input而不是change.

Maybe some difference in the event ordering relative to focus?

推荐答案

According to this post:

  • 100通过用户界面更改元素的文本内容时发生事件.

  • 100在元素have changed的 Select 、选中状态或内容时发生.在某些情况下,只有当元素失go 焦点或按Return(Enter)并且值已更改时,才会发生这种情况.onchange属性可以与:<input><select><textarea>一起使用.

TL;DR:

  • oninput: any change made in the text content
  • onchange:
    • If it is an <input />: change + lose focus
    • If it is a <select>: change option

$("input, select").on("input", function () {
    $("pre").prepend("\nOn input. | " + this.tagName + " | " + this.value);
}).on("change", function () {
    $("pre").prepend("\nOn change | " + this.tagName + " | " + this.value);
}).on("focus", function () {
    $("pre").prepend("\nOn focus | " + this.tagName + " | " + this.value);
}).on("blur", function () {
    $("pre").prepend("\nOn blur | " + this.tagName + " | " + this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
<select>
  <option>Alice</option>
  <option>Bob</option>
  <option>Carol</option>
  <option>Dave</option>
  <option>Emma</option>
</select>
<pre></pre>

Jquery相关问答推荐

为什么对同一个 ASP.NET MVC 操作的多个同时 AJAX 调用会导致浏览器阻塞?

如何在 jQuery 中 Select 特定的表单元素?

在页面上放置文件时如何设置文件输入值?

Google Maps API v3 infowindow 关闭事件/回调?

调用 e.preventDefault() 后提交表单

Select 除第一个之外的所有子元素

如何使用 jQuery 为文本对齐动态添加样式

使用 时如何从 select2 中获取选定文本

jQuery:获取父母,父母ID?

页面重新加载后,如何使用 twitter bootstrap 保持当前选项卡处于活动状态?

使用 JQuery 获取触发事件的元素的类

如何检测 window.print() 完成

如何使用 jQuery 停止默认链接点击行为

jQuery - 确定输入元素是文本框还是 Select 列表

如何使用 jQuery 取消设置元素的 CSS 属性?

jQuery: Select 不为空的数据属性?

jQuery或javascript查找页面的内存使用情况

Twitter Bootstrap alert 消息关闭并再次打开

带有 LIKE 的 Spring JPA @Query

清除表jquery