简体   繁体   中英

Jquery live function suddenly not working anymore

I was using jQuery live() function to detect when the user was pressing some special keys (arrows, etc.):

$('.TextBox1').live('keydown', function(e) {
    var keyCode = e.keyCode || e.which;
    if (keyCode == 40) {
        e.preventDefault();
        // ...
    }
});

It was working fine for a couple of months when suddenly yesterday it stopped working, preventing every line of JS code below it from executing. I have tried to replace it with the .keyup function but this has not helped. What could have happened?

I assume you've updated to the latest version of jQuery? live() has been deprecated since jQ1.7, and is now removed as of 1.9.

Instead, you should use on() with a delegate parameter:

$(document).on('keydown', '.TextBox1', function(e) {
    var keyCode = e.keyCode || e.which;

    if (keyCode == 40) {
        e.preventDefault();
        //...
    }
});

Note that for best performance you should replace document in the above example with the closest parent element of .TextBox1 which is not dynamically appended to the DOM after page load.

jQuery .live is deprecated. Although not strictly the same, use .on() For your example it would look like:

$('body').on('keydown', '.TextBox1', function() {
    var keyCode = e.keyCode || e.which;
    if (keyCode == 40) {
        e.preventDefault();
        ...
    }
}

.live() was removed after jQuery 1.9.

You have to use .on from now on.

.live() was removed in the latest version of jQuery 1.9.

You can use the jQuery Migrate plugin instead, or change the code to use .on .

.live was removed in jQuery 1.9. If you are calling the lastest version of jQuery it will break.

Change your code to use .on

use this as mentioned on jquery site. for your example it will be

$(document).on( "keydown", ".TextBox1", function() { });

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

Read this for more information about live

live has actually been removed on jq 1.9, not just deprecated! you can use the plugins for compatibility or update your code to on

.live() deprecated in jq 1.9 so you can try this

$('.TextBox1').on('keydown', function() {
    var keyCode = e.keyCode || e.which;
    if (keyCode == 40) {
        e.preventDefault();
        ...
    }
}

Have you updated the jquery to 1.7?

As of jQuery 1.7, the .live() method is deprecated.

Use .on() to attach event handlers:

$('.TextBox1').on('keydown', text_box_key_down_function);

You can also use bind:

$('.TextBox1').bind('keydown',text_box_key_down_function);

.live was removed when JQ 1.9 was released, so I guess you have updated Jquery to the lastest.

Now, try:

$('.TextBox1').on('keydown', function() {
    var keyCode = e.keyCode || e.which;
    if (keyCode == 40) {
        e.preventDefault();
        ...
    }
}

.live was removed when JQ 1.9 but now there is solution for run both .live and .on at the same time

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>

More about Jquery migrate

Feather information from stackoverflow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM