Domanda

Is it possible to bind keyup() to an entire <form>? For example, I have multiple <input type="text"> fields inside a form, and I want to bind a keyup event to all of them without having to attach a keyup event handler to each one. Rather, I want to bind one keyup event handler to all of them so if any of the text fields detect a keyup event, the event handler will run. Is this possible in jQuery?

I tried:

$('form').keyup(function() {...});

But that made the keyup event handler fire indefinitely after typing one character.

È stato utile?

Soluzione

I don't think this is possible, but you are able to bind the same keyup event to all the inputs at once:

$('#myform input[type=text]').on('keyup', /* yada */);

Altri suggerimenti

You may try something Like this: Fast Demo

$(function(){
   //create one instance for handler:
   var myHandler = function(e){ /* Your function */ };

   //Bind it:
   $("#form input[type=text]").on('keyup',function(e){ myHandler(e);  });

});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top