2

I'm trying to implement a Tab key listener for a textbox.

$('#mytextbox').live('keydown', function (e) {
   if (e.keyCode == 9 || e.which == 9) {
      // TO DO SOMETHING
   }
});

However, for some reason I need to limit the tab listener's callback to invoke only when the textbox has changed. Is there anyway to do this?

4

3 回答 3

1

您也许可以检查输入字段的值以确保它与原始值不同?

例如

$('#mytextbox').live('keydown', function (e) {
   if ((e.keyCode == 9 || e.which == 9) && ($('#TextBox').val() != 'Starting Value')) {
      // TO DO SOMETHING
   }
});
于 2013-04-21T08:51:54.597 回答
0

或者不存储/不知道该文本框的值:

var changed = false;

$('#mytextbox').on('keydown', function (e) {
    if (e.which == 9 && changed) {
        e.preventDefault();
        // TO DO SOMETHING
        alert("works");
        changed = false;
    } else {
        changed = true;
    }
});

http://jsfiddle.net/9a37b/

于 2013-04-21T09:12:36.823 回答
0

你可以这样做:

var data = "";
$('#mytextbox').live('keydown', function (e){
    if(e.which == 9 || e.keyCode == 9){
         if($(this).val() != data){
             alert('changed!');
             data = $(this).val();
         }
    }
});

http://jsfiddle.net/DDCZS/1/

于 2013-04-21T09:02:41.213 回答