Capture “Enter” keypress with numeric input in And

2019-04-07 16:06发布

I am creating an Android application using Cordova/PhoneGap 2.5.0, Knockout 2.1.0 and jQuery Mobile 1.3.0.

I have created an input with type "number", this input is databound with Knockout on it's value. It is also databound to a key press event. I'm intending to catch the user pressing the enter key.

<input type="number" data-bind="value: $root.myInputValue, event: { keypress: $root.myInputKeypress }" min="0" step="1" max="29">


self.myInputKeypress = function() {
    var keyCode = (event.which ? event.which : event.keyCode);

    alert(keyCode);

    if (keyCode === 13) {
        //Do work here

        return false;
    }

    return true;
};

When I press an allowed key such as a number the code runs as expected and the pressed keys code is alerted. When I press enter on the keyboard nothing happens, it seems that Android is suppressing events from keys it thinks are not relevant.

Is there a way to change this behaviour so that I can capture the user hitting enter?

4条回答
老娘就宠你
2楼-- · 2019-04-07 16:39

If you are using Ionic/Angular

<input ng-keypressed="myKeyPressed($event)" ng-model="myField">

self.myKeyPressed = function(keyEvent) {
    if(keyEvent.keyCode == 13) //do things
}
查看更多
SAY GOODBYE
3楼-- · 2019-04-07 16:41

I had the same issue, the problem was with number field the keycode for enter is 9 on my device, on simulator it is 13.

This works for me:

$('.ui-input-text').live('keyup', function(event) {
if (event.keyCode == 9) {
    // 'Go' pressed do something
}});
查看更多
爷、活的狠高调
4楼-- · 2019-04-07 16:57

try this could work in your situation using jquery mobile

$('.ui-input-text').live('keyup', function(event) {
if (event.keyCode == 13) {
    // 'Go' pressed do something
}});
查看更多
迷人小祖宗
5楼-- · 2019-04-07 16:57

hi Put your text box in form tag

 <form id="Articleform" name="Article">
                          <input type="number" data-bind="value: $root.myInputValue, event: { keypress: $root.myInputKeypress }" min="0" step="1" max="29">
                        </form>

and add event form submit tage

$(document).ready(function () {
    $("#Articleform").on('submit', function (event) {
        alert("enter");
        return false;
    });

when you click on go alert is open

查看更多
登录 后发表回答