首页 > 解决方案 > type="number"时获取input的ENTER点击事件

问题描述

在 HTML输入文本中,使用onKeyUp方法我能够在输入类型=“文本时捕获输入按钮上的单击事件

但是如果输入type="number",则无法触发相同的事件。

HTML

<input id="inputLocation" type="text" class="inputBarCode" style="text-transform: uppercase" placeholder: placeholder/>

JS

 $('#inputLocation').keyup(function (e) {
     if (e.which === 13) {
         $('#inputLocation').blur();
         //self.executeLocationLookup();
         alert("Event ", e.which);
     }
 });

如果输入类型是数字键盘,你能告诉我如何获取输入按钮的点击事件(13)

请参考附图

标签: javascripthtmljqueryinputonkeyup

解决方案


使用event.target.value财产或$(this).val()

$('#inputLocation').keyup(function (e) {
  if (e.which === 13 && isNumber(e.target.value)) {
     $('#inputLocation').blur();
     //self.executeLocationLookup();
     alert("Event ", e.which);
  }
});  

function isNumber(n) {
   return !isNaN(parseFloat(n)) && isFinite(n);
}

推荐阅读