jQuery 多个事件触发相同的功能

有没有一种方法可以使keyupkeypressblurchange事件在一行中调用同一功能,还是我必须分别进行处理?

我的问题是,我需要使用 db 查找来验证某些数据,并且想要确保无论在键入还是粘贴到框中的情况下,都不会丢失验证。

答案

$('#element').on('keyup keypress blur change', function(e) {
    // e.type is the type of event fired
});
var myFunction = function() {
   ...
}

$('#element')
    .keyup(myFunction)
    .keypress(myFunction)
    .blur(myFunction)
    .change(myFunction)
$(document).on('mouseover mouseout',".brand", function () {
  $(".star").toggleClass("hovered");
})
$('#element').on('keyup keypress blur change', function(event) {
    alert(event.type); // keyup OR keypress OR blur OR change
});
$('#foo').bind('mouseenter mouseleave', function() {
  $(this).toggleClass('entered');
});
$('#ValidatedInput').keydown(function(evt) {
  // If enter is pressed
  if (evt.keyCode === 13) {
    evt.preventDefault();

    // If changes have been made to the input's value, 
    //  blur() will result in a change event being fired.
    this.blur();
  }
});

$('#ValidatedInput').change(function(evt) {
  var valueToValidate = this.value;

  // Your validation callback/logic here.
});
$("input[name='title']").on({
    "change keyup": function(e) {
        var slug = $(this).val().split(" ").join("-").toLowerCase();
        $("input[name='slug']").val(slug);
    },
});
var foo = function() {...}
$('#selector').on('keyup keypress blur change paste cut', foo);
function myFunction() {
    ...
}
// Call individually due to IE not handling binds properly
$(window).on("scroll", myFunction);
$(window).on("resize", myFunction);
$("element").on("event1 event2 event..n", function() {
   //execution
});
$('#target').on('keyup keypress blur change', function(e) {
    // "e" is an event, you can detect the type of event using "e.type"
});