Reputation: 76
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?
Upvotes: 6
Views: 4560
Reputation: 7554
If you are using Ionic/Angular
<input ng-keypressed="myKeyPressed($event)" ng-model="myField">
self.myKeyPressed = function(keyEvent) {
if(keyEvent.keyCode == 13) //do things
}
Upvotes: 1
Reputation: 1
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
}});
Upvotes: 0
Reputation: 3294
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
Upvotes: 0
Reputation: 11
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
}});
Upvotes: 0