javascript jquery mobile - 移动设备上的 keyup keydown

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21113050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:06:05  来源:igfitidea点击:

jquery mobile - keyup keydown on mobile devices

javascriptandroidjquery-mobileonkeyup

提问by Nigel Johnson

I have a need for an input (type='text') to go send results to the my server to check availability of something typed by the user.

我需要一个输入 (type='text') 将结果发送到我的服务器以检查用户输入的内容的可用性。

I use the delegate to add the event handlers to the elements:

我使用委托将事件处理程序添加到元素:

$(document).delegate('#signup', 'pageshow', function() {
    var keydown = function(e) {
        e = e || window.event;
        var char = e.which || e.keyCode;
        if (char == 8) {
            $(".pagemessage").text("Pressed: '<BACKSPACE>'");
            appcheckDomainOnKeyDown();
        }
        return true;
    };

    var keyup = function(e) {
        e = e || window.event;
        var char = e.which || e.keyCode;
        if (char == 8) {
            appcheckDomainOnKeyUp();
        }
        return true;
    };

    var keypress = function(e) {
        e = e || window.event;
        var char = e.which || e.keyCode;
        var str = String.fromCharCode(char);
        $(".pagemessage").text("Pressed: '" + str +"'");
        if (/[a-zA-Z0-9-\._]/.test(str) || char == 8 || char == 9) {
            appcheckDomainOnKeyDown();
            appcheckDomainOnKeyUp();
            return true;
        }
        return false;
    };

The key handers work perfectly on my desktop but not on a mobile device. Hopefully you can see that I'm trying to allow certain characters into the box (and a backspace to delete the characters.

关键处理程序在我的桌面上可以完美运行,但在移动设备上却不能。希望您能看到我正在尝试允许某些字符进入框中(以及用于删除字符的退格。

From the fact I cannot see the pagemessage element update, 'keypress' does not seem to be trapped. I tried handling this in the keyup/keydown, but I'm not sure how to apply the shiftKey bits to get an actual character pressed - for example pressing + 5 would give '%' however in the keydown it returns shiftKey and 5.

从我看不到 pagemessage 元素更新的事实来看,'keypress' 似乎没有被困住。我尝试在 keyup/keydown 中处理这个问题,但我不确定如何应用 shiftKey 位来获得按下的实际字符 - 例如按 + 5 会给出 '%' 但是在 keydown 中它返回 shiftKey 和 5。

I read the documentation and the closest I could find to 'keypress' was a 'tap' event, but that didn't work either.

我阅读了文档,我能找到的最接近 'keypress' 的是一个 'tap' 事件,但这也不起作用。

I have tried trapping the 'keypress' event as suggested in one post here, and on a desktop this does not trap the backspace, and does nothing at all on a mobile device.

我曾尝试按照此处的一篇文章中的建议捕获“keypress”事件,在台式机上这不会捕获退格键,并且在移动设备上根本不执行任何操作。

I then tried this as suggested in another post:

然后我按照另一篇文章的建议尝试了这个:

    var inputEV = 'oninput' in window ? 'input' : 'keyup';
    $("#new_domain").off(inputEV);
    $("#new_domain").on(inputEV, function (e) {
        keydown(e);
        keyup(e);
    });

and it does not work in either desktop browser or mobile device.

它不适用于桌面浏览器或移动设备。

I then tried changing the input type to 'search', and I get a pretty enhancement, that a keypress does add a clear button... but does nothing on the mobile device regarding my own functionality.

然后我尝试将输入类型更改为“搜索”,我得到了一个很好的增强,按键确实添加了一个清除按钮......但在移动设备上没有任何关于我自己的功能的功能。

I think I have run out of things to try, the only thing left is to add a button to go check - and no one wants that :)

我想我已经没有什么可以尝试的了,唯一剩下的就是添加一个按钮来检查 - 没有人想要:)

Anyone know how I can do what I need?

有谁知道我怎么做我需要的?

In case it's relevant, I'm using chrome on my desktop and android device (HTC one, and Nexus 5)

如果相关,我在台式机和安卓设备(HTC 和 Nexus 5)上使用 chrome

回答by shanabus

Keyup should work. It works in this example: http://jsbin.com/aNEBIKA/2/. That tested find on my Galaxy S3. Each keypress updates the footer h3element with the text entered.

Keyup 应该可以工作。它适用于这个例子:http: //jsbin.com/aNEBIKA/2/。在我的 Galaxy S3 上测试发现。每次按键都会h3使用输入的文本更新页脚元素。

Could it be that you are binding your listeners at the wrong time? The documentation does suggest binding like this:

会不会是你在错误的时间绑定了你的听众?文档确实建议这样绑定:

$(document).bind('pageinit')

http://demos.jquerymobile.com/1.2.0/docs/api/events.html

http://demos.jquerymobile.com/1.2.0/docs/api/events.html