【问题标题】:Get the ENTER click event of input when type="number"type="number"时获取input的ENTER点击事件
【发布时间】:2018-05-03 14:36:56
【问题描述】:

在 HTML input text 中,使用 onKeyUp 方法我可以在输入 时捕获输入按钮上的 click 事件 类型="文本" 。

但如果输入 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)

【问题讨论】:

  • Android 8 上的 Chrome 返回 13,正如您所料。

标签: javascript html jquery input onkeyup


【解决方案1】:

使用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);
}

【讨论】:

    【解决方案2】:

    最好实际监听submit事件,该事件在按下回车键时触发。

    使用以下标记:

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

    您可以使用submit() 来监听提交事件。

    $('form').submit(function (e) {
        // prevent the page from auto-navigating on form submit
        e.preventDefault();
    
        // do something
    });
    

    【讨论】:

      猜你喜欢
      • 2011-04-25
      • 2011-11-08
      • 2013-09-22
      • 2016-05-24
      相关资源
      最近更新 更多