jQuery: How to capture the TAB keypress within a Textbox

前端 未结 9 1473
迷失自我
迷失自我 2020-11-30 19:32

I want to capture the TAB keypress, cancel the default action and call my own javascript function.

相关标签:
9条回答
  • 2020-11-30 19:54

    Above shown methods did not work for me, may be i am using bit old jquery, then finally the below shown code snippet works for - posting just in case somebody in my same position

    $('#textBox').live('keydown', function(e) {
        if (e.keyCode == 9) {
            e.preventDefault();
            alert('tab');
        }
    });
    
    0 讨论(0)
  • 2020-11-30 20:02

    Working example in jQuery 1.9:

    $('body').on('keydown', '#textbox', function(e) {
        if (e.which == 9) {
            e.preventDefault();
            // do your code
        }
    });
    
    0 讨论(0)
  • 2020-11-30 20:02

    You can capture an event tab using this JQuery API.

    $( "#yourInputTextId" ).keydown(function(evt) {
       if(evt.key === "Tab")
          //call your function
    });
    
    0 讨论(0)
提交回复
热议问题