【问题标题】:Detect keydown event (mobile)检测 keydown 事件(移动)
【发布时间】:2023-03-10 14:37:01
【问题描述】:

我目前正在使用cordova 开发一个移动应用程序。我遇到了这个问题,我编写了一个函数来检测用户何时在文本输入字段中输入并按下回车键。

这是 JavaScript sn-p;

<script type="text/javascript">
    $(function () {
        $("#searchForm").on("input", function (e) {
            e.preventDefault();
        });
        $("input").on("keydown", function (e) {
            if (e.keyCode === 13) { 
                var keyField = document.createElement('input');
                keyField.setAttribute('type', 'text');
                document.body.appendChild(keyField);
                setTimeout(function () {
                    keyField.focus();
                    setTimeout(function () {
                        keyField.setAttribute('style', 'display:none;');
                    }, 50);
                }, 50);
            }
        });
    });
</script>

HTML 部分;

<form onsubmit="return false" id="searchForm">
    <input type="text" id="fieldOne">
    <input type="text" id="fieldTwo">
    <input type="button" value="Search" id="search" onclick="executeSearches()"/>
</form>      

此代码用于实现隐藏移动键盘,但是当隐藏功能执行时,由于keyField -变量,视图正在移动。

有没有其他方法可以在不移动视图的情况下实现此功能,或者我可以以某种方式确定视图被移动到的位置?

【问题讨论】:

    标签: javascript jquery html cordova


    【解决方案1】:

    你缺少“#”,检查 jquery 选择器

    $("#searchForm").on("input", function (e) {
            e.preventDefault();
        });
    

    【讨论】:

    • 注意到了,但它仍然没有改变按下“回车”键后视图正在移动的事实。有什么想法可以解决这个问题?
    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2013-08-23
    相关资源
    最近更新 更多