简体   繁体   English

iPhone Mobile Safari:强制打开键盘

[英]iPhone Mobile Safari: Force keyboard open

This is an HTML / CSS / JS (jQuery) iPad app. 这是一个HTML / CSS / JS(jQuery)iPad应用程序。 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. 我想将用户集中在输入上,然后启动键盘。

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)? 有没有办法强制或模拟用户点击输入(或任何事情)?

Maybe these links can help you: 也许这些链接可以帮助您:

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);

});

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();
}

Try: 尝试:

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

or if that does not work: 或者如果不起作用:

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

blur() eliminates the focus and focus() brings it back. blur()消除了焦点,焦点()消除了它。 For some reason, it works every time compared to focus() working once in a while on its own. 出于某种原因,每次与focus()自己一次工作相比,它每次都有效。

这应该做你想要的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM