Behavior of “enter/next” key on mobile browsers when entering into a number field

眉间皱痕 提交于 2019-12-21 05:31:25

问题


I've got a single line of code (no forms involved) here:

<input type="number" min="0" max="9999999" id="inSku" name="inSku">

Before, I was using type="text", but on mobile browsers that gave the full keyboard by default and the box in question only takes number inputs. Logically, the mobile browsers will change to an only-number keyboard when they focus on a type="number" field - however, I am currently relying on a little bit of jQuery to handle the submission of the content of the form. Here's that code:

$("#inSku").keyup(function (event) {
    if (event.keyCode == 13) {
        ringItem();
    }
});

The problem here being that on the stock android phone keyboard (and I'm assuming a good number of other mobile browsers) - when using the "number" keyboard, the enter key that is on the regular alphanumeric android keyboard has changed to a "next" button. This button in my case takes the text input and sticks it in the address field, not exactly ideal. Is there any way to harness the next button on the keyboard and get that to execute my other javascript function?


回答1:


Try

 $('#inSku').keyup(function (e) {
   alert('Key pressed: ' + e.keyCode);
 });

The idea here is to press the next button and find that key code. Once you do, you can add it to your logic like if (event.keyCode === 13 || event.keyCode === ...). You can add as many qualifiers as you need to to support multiple keypads and devices.



来源:https://stackoverflow.com/questions/19016548/behavior-of-enter-next-key-on-mobile-browsers-when-entering-into-a-number-fiel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!