JQuery.live ('click') works even when buttons are disabled

I use the latest versions (jQuery 1.7.1 and jQuery UI 1.8.16) of all libraries, but the buttons that I bound the .live event ('click') to still fire even when the button is disabled. When I bind a regular .click () event to it, it does not fire.

Any suggestions? I think that I am either using .live () incorrectly, or it is a mistake in the jQuery user interface.

Here is an example JSFiddle. http://jsfiddle.net/Kb66j/1/

Thanks! Alex

+6
source share
3 answers

Starting with version 1.7,. live () is deprecated .

"Starting with jQuery 1.7, the .live () method is deprecated. Use . On () to attach event handlers."

In addition, starting with version 1.7 . on () is preferred .bind ()

"For jQuery 1.7, the .on () method is the preferred method for attaching event handlers to a document."

+2
source

This is because live uses a burst of events. When you click on a disabled button, the event is still bubbling, so live fires the event, and all click event handlers are executed. You can check the enabled state of the button inside the handler before doing anything. try it

 $("#the_button").button({ icons: { primary: "ui-icon-disk" }, disabled: true }).live('click',function () { if($(this).is(':enabled')){ alert('clicked'); } }); 

Job Demo

Also, as indicated by other live , is deprecated from version 1.7+, so you can try and use on , but this problem will still be present, and you will have to handle it as I described above.

+6
source

live outdated

use the new on () method instead

0
source

Source: https://habr.com/ru/post/906326/


All Articles