jQuery iPhone Mobile Safari:强制打开键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3227291/
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-08-26 15:03:09  来源:igfitidea点击:

iPhone Mobile Safari: Force keyboard open

jqueryiphoneipadmobilesafari

提问by KrazyKen04

This is an HTML / CSS / JS (jQuery) iPad app. I've got a button, that slides down an input form. I'd like to focus the user on the input, and then launch the keyboard.

这是一个 HTML / CSS / JS (jQuery) iPad 应用程序。我有一个按钮,可以滑下输入表单。我想将用户集中在输入上,然后启动键盘。

This is what I'm working with, but doesn't work:

这是我正在使用的,但不起作用:

$('#myFormField').focus();

This does indeed focus the input, but fails to launch the keyboard. Is it just not possible (for security reasons or whatever)?

这确实聚焦输入,但无法启动键盘。是不是不可能(出于安全原因或其他原因)?

Is there a way to force or simulate a user tapping the input (or anything for that matter)?

有没有办法强制或模拟用户点击输入(或任何与此相关的内容)?

回答by Sylter

回答by jamy

Try this

尝试这个

Tested on Safari iPhone 7 & iPhone 6 Plus, Android 4.4

在 Safari iPhone 7 & iPhone 6 Plus、Android 4.4 上测试

and it opened iOS virtual keyboard successfully when I touched the button.

当我触摸按钮时,它成功打开了 iOS 虚拟键盘。

condition

健康)状况

1) make a Button to trigger keypress event => $('.btn_abc')

1)制作一个按钮来触发按键事件=> $('.btn_abc')

2) make a text input tag, and set the ID to this element => $('#search_input')

2)制作一个文本输入标签,并将ID设置为该元素=> $('#search_input')

$( document ).ready(function() {

  var focusTextField = function(){
    console.log("focusElement");
  };

  var onKeypressHandler = function(){
    console.log("* * * keypress event handler")
    $('#search_input').blur().focus();
  };

  var onClickHandler = function() {
    $('.btn_abc').trigger('keypress');
  };

  $('#search_input').bind('focus', focusTextField);
  $('.btn_abc').bind('click', onClickHandler);
  $('.btn_abc').bind('keypress', onKeypressHandler);

});

回答by user2638216

Turn that button into an input field then instantly swap focus to the main input field. They keyboard will open and stay opened. Below is an example using a button like you asked, placing a transparent input field over the button the user clicks. Tested on an iPhone 4 (iOS 6/7) and an HTC Windows phone.

将该按钮变成输入字段,然后立即将焦点切换到主输入字段。他们的键盘将打开并保持打开状态。下面是一个使用您所要求的按钮的示例,在用户单击的按钮上放置一个透明的输入字段。在 iPhone 4 (iOS 6/7) 和 HTC Windows 手机上测试。

The input you want the user to focus on:

您希望用户关注的输入:

<input id="main" />

The button the user presses (with input field overlayed using css):

用户按下的按钮(使用 css 覆盖输入字段):

<a>Button</a><input onfocus="FocusMain()" />

Swap focus instantly:

立即切换焦点:

function FocusMain()
{
    $("#main").focus();
}

回答by Collective Cognition

This should do what you want:

这应该做你想做的:

$("#element").focus().click();

回答by Bobby

Try:

尝试:

$('#element').blur().focus();

or if that does not work:

或者如果这不起作用:

$('#element').focus().blur().focus();

blur() eliminates the focus and focus() brings it back. For some reason, it works every time compared to focus() working once in a while on its own.

blur() 消除了焦点,focus() 将其带回来。出于某种原因,与 focus() 偶尔单独工作相比,它每次都有效。