jQuery multiple events to trigger the same function

后端 未结 11 1073
青春惊慌失措
青春惊慌失措 2020-11-22 04:45

Is there a way to have keyup, keypress, blur, and change events call the same function in one line or do I have to do the

相关标签:
11条回答
  • 2020-11-22 05:26

    If you attach the same event handler to several events, you often run into the issue of more than one of them firing at once (e.g. user presses tab after editing; keydown, change, and blur might all fire).

    It sounds like what you actually want is something like this:

    $('#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.
    });
    
    0 讨论(0)
  • 2020-11-22 05:29

    I was looking for a way to get the event type when jQuery listens for several events at once, and Google put me here.

    So, for those interested, event.type is my answer :

    $('#element').on('keyup keypress blur change', function(event) {
        alert(event.type); // keyup OR keypress OR blur OR change
    });
    

    More info in the jQuery doc.

    0 讨论(0)
  • 2020-11-22 05:30

    You can use .on() to bind a function to multiple events:

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

    Or just pass the function as the parameter to normal event functions:

    var myFunction = function() {
       ...
    }
    
    $('#element')
        .keyup(myFunction)
        .keypress(myFunction)
        .blur(myFunction)
        .change(myFunction)
    
    0 讨论(0)
  • 2020-11-22 05:30

    This is how i do it.

    $("input[name='title']").on({
        "change keyup": function(e) {
            var slug = $(this).val().split(" ").join("-").toLowerCase();
            $("input[name='slug']").val(slug);
        },
    });
    
    0 讨论(0)
  • 2020-11-22 05:34

    The answer by Tatu is how I would intuitively do it, but I have experienced some problems in Internet Explorer with this way of nesting/binding the events, even though it is done through the .on() method.

    I havn't been able to pinpoint exactly which versions of jQuery this is the problem with. But I sometimes see the problem in the following versions:

    • 2.0.2
    • 1.10.1
    • 1.6.4
    • Mobile 1.3.0b1
    • Mobile 1.4.2
    • Mobile 1.2.0

    My workaround have been to first define the function,

    function myFunction() {
        ...
    }
    

    and then handle the events individually

    // Call individually due to IE not handling binds properly
    $(window).on("scroll", myFunction);
    $(window).on("resize", myFunction);
    

    This is not the prettiest solution, but it works for me, and I thought I would put it out there to help others that might stumble upon this issue

    0 讨论(0)
  • 2020-11-22 05:37

    You can use bind method to attach function to several events. Just pass the event names and the handler function as in this code:

    $('#foo').bind('mouseenter mouseleave', function() {
      $(this).toggleClass('entered');
    });
    

    Another option is to use chaining support of jquery api.

    0 讨论(0)
提交回复
热议问题