最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

enter - How to call function when I press tab key in javascript? - Stack Overflow

programmeradmin4浏览0评论

I have a function like that :

function whenEmpty(field) {
  if (field.value == '') {
     field.style.backgroundColor = "#ffcccc";
     alert("Please fill the field.");
     field.focus();
  } else {
     field.style.backgroundColor = "#ffff80";
  }
}

Before I thought about that idea (I want to call this function when tab key is pressed. ) I call the function on attribute onblur.

<tr>
  <td>
    <div class="required">
      <label>Phone</label>
    </div>
  </td>
  <td>
    <form:input path="connote.destPhone" id="destPhone" htmlEscape="true" onfocus="this.select();" onmouseup="return false;" onblur="whenEmpty(this);" style="background-color:#ffff80" size="20" maxlength="50" tabindex="9" />
    <form:errors path="connote.destPhone" cssClass="error" />
  </td>
</tr>

But now I want call that function only when I press tab.

I have a function like that :

function whenEmpty(field) {
  if (field.value == '') {
     field.style.backgroundColor = "#ffcccc";
     alert("Please fill the field.");
     field.focus();
  } else {
     field.style.backgroundColor = "#ffff80";
  }
}

Before I thought about that idea (I want to call this function when tab key is pressed. ) I call the function on attribute onblur.

<tr>
  <td>
    <div class="required">
      <label>Phone</label>
    </div>
  </td>
  <td>
    <form:input path="connote.destPhone" id="destPhone" htmlEscape="true" onfocus="this.select();" onmouseup="return false;" onblur="whenEmpty(this);" style="background-color:#ffff80" size="20" maxlength="50" tabindex="9" />
    <form:errors path="connote.destPhone" cssClass="error" />
  </td>
</tr>

But now I want call that function only when I press tab.

Share Improve this question edited Jan 29, 2017 at 9:38 Hakan Fıstık 19.6k16 gold badges124 silver badges147 bronze badges asked Jan 26, 2016 at 8:11 kumala nindyakumala nindya 691 gold badge1 silver badge11 bronze badges 1
  • Possible duplicate of Capturing TAB key in text box – squaleLis Commented Jan 26, 2016 at 8:19
Add a ment  | 

2 Answers 2

Reset to default 5

You can add an event listener to the document like so.

document.addEventListener('keyup', function(event) {
  if (event.keyCode == 9) {
     whenEmpty();
  }
});

This will listen for any keyup (keyboard button released) events, and then in the if check what the keycode of the pressed button was. 9 is the keycode for Tab. If you want to pass the field to the function, you could also add the event listener to the input itself. Then access it with event.target

You can add a keyup listener on body or you can add listener on your table(just if you add tabindex attribute )

// event listener for keyup
function checkTabPress(e) {        
    if (e.keyCode == 9) {
     //call your function whenEmpty()
    }
}

var body = document.querySelector('body');
body.addEventListener('keyup', checkTabPress);
发布评论

评论列表(0)

  1. 暂无评论