1

I came across code that adds custom events to detect input text changes originating from either a keyboard or mouse event:

http://www.zurb.com/playground/jquery-text-change-custom-event

The code is a bit older, and the documentation says to use bind() to attach the custom events, e.g.:

$('#exhibita').bind('hastext', function () {
  $('#exhibitaButton').removeClass('disabled').attr('disabled', false);
});

The jQuery docs state

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements.

I thought I could just rewrite the code as:

$(document).on('hastext', '#exhibita', function () {
  $('#exhibitaButton').removeClass('disabled').attr('disabled', false);
});

but the event handler is never triggered.

Questions

  • Did I make a mistake in my use of on()?
  • Must the code providing the custom events be rewritten to support on()? If so, what must change?
  • Is bind() documented to be a candidate for removal in a future version of jQuery? I did not see a depreciation warning in the docs, but I know live() and die() were removed from 1.9...
  • Is there a future-proof way to use these custom events without rewriting the code?
4

1 に答える 1

1

The way you are using on here is creating even delegation. This is basically equivalent to the old jquery live.

It is possible that this custom event doesn't bubble properly. Try this instead:

$('#exhibita').on('hastext', function () {

If bind works, this will work.

于 2013-03-11T21:06:15.547 に答える