繁体   English   中英

以编程方式关注移动 Safari 中的下一个输入字段

[英]Programmatically focus on next input field in mobile safari

我有几个输入字段,就像填字游戏的答案行一样:

在此处输入图片说明

每个方块都有自己的输入字段。 其原因之一是有时可以预先填充正方形。 现在,在桌面浏览器上,只要输入一个字符,光标就会跳转到下一个输入字段。 使用以下内容非常有效:

$(this).next('input').focus();

但是移动 safari 上的问题(我们在 ios 上测试)是我不知道如何以编程方式自动“跳转”到下一个输入字段。 用户可以通过“下一步”按钮执行此操作,但有没有办法自动执行此操作?

我知道focus()触发器对 ios 有一些限制,但我也看到了一些使用合​​成点击等的解决方法。

我找到了一个可能对你有用的解决方法。

显然, IOS/Safari 仅在触摸事件处理程序中“接受”焦点。 我触发了一个触摸事件并将.focus()插入其中。 我用 Safari 在我的 iPhone3S 和 iPhone5S 上试过这个,它工作正常:

var focused = $('input:first'); //this is just to have a starting point

$('button').on('click', function () { // trigger touch on element to set focus
    focused.next('input').trigger('touchstart'); // trigger touchstart
});

$('input').on('touchstart', function () {
    $(this).focus();   // inside this function the focus works
    focused = $(this); // to point to currently focused
});

演示在这里
(在演示中按下一步按钮)

在不关闭键盘的情况下以编程方式移动到移动浏览器中的下一个输入字段似乎是不可能的。 (这是一个糟糕的设计,但这是我们必须使用的。)然而,一个聪明的技巧是用 Javascript 交换输入元素的位置、值和属性,这样看起来你实际上是在移动到下一个字段你仍然专注于同一个元素。 下面是用于交换idname和 value 的 jQuery 插件的代码。 您可以根据需要调整它以交换其他属性。 还要确保修复所有已注册的事件处理程序。

$.fn.fakeFocusNextInput = function() {
    var sel = this;
    var nextel = sel.next();
    var nextval = nextel.val();
    var nextid = nextel.attr('id');
    var nextname = nextel.attr('name');
    nextel.val(sel.val());
    nextel.attr('id', sel.attr('id'));
    nextel.attr('name', sel.attr('name'));
    sel.val(nextval);
    sel.attr('id', nextid);
    sel.attr('name', nextname);
    // Need to remove nextel and not sel to retain focus on sel
    nextel.remove();
    sel.before(nextel);
    // Could also return 'this' depending on how you you want the
    // plug-in to work
    return nextel;
};

演示在这里: http : //jsfiddle.net/EbU6a/194/

UIWebview 有属性keyboardDisplayRequiresUserAction

当此属性设置为true ,用户必须显式点击 Web 视图中的元素以显示该元素的键盘(或其他相关输入视图)。 当设置为false ,元素上的焦点事件会导致显示输入视图并自动与该元素关联。

https://developer.apple.com/documentation/uikit/uiwebview/1617967-keyboarddisplayrequiresuseractio

我希望这就是你要找的——

$(document).ready(function() {
  $('input:first').focus(); //focus first input element

  $('input').on('keyup', function(e) {
    if(e.keyCode == 8) {  //check if backspace is pressed
      $(this).prev('input').focus(); 
      return;
    }
    if($(this).val().length >= 1) { //for e.g. you are entering pin
      if ($(this).hasClass("last")) {
        alert("done");
        return;
      }
      $(this).next('input').focus(); 
    }
  });

  $('input').on('focus', function() {
    if(!$(this).prev('input').val()){
      $(this).prev('input').focus();
    }
  });
});

在这里检查代码-

https://jsbin.com/soqubal/3/edit?html,output

在 ios 部分的配置文件中添加这一行

首选项名称="KeyboardDisplayRequiresUserAction" value="false"

我在 safari ios 上遇到了同样的问题。 在登录页面上,我以编程方式关注用户输入一个短信代码后的下一个输入字段。 我触发了对输入更改事件的自动聚焦。 我通过添加延迟解决了这个问题。

详情请看下面的代码

 <InputItem value={item} type='number' maxLength={1} ref={el => this[`focusInstRef${index}`] = el} onChange={(value) => { value&&index !== 5 && setTimeout(() => {this[`focusInstRef${index + 1}`].focus()}, 5); vAppStore.setSmsCodeArr(value, index);} } />

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
     #hidebox {position:absolute; border: none; background:transparent;padding:1px;}
     #hidebox:focus{outline: 0;}

    </style>
</head>

<body>

<input type="text" maxlength="1" onkeyup="keyUp(this)" onkeydown="keyDown(this)" size="2" id="hidebox" at="1">
<input type="text" maxlength="1" size="2" id="mFirst" at="1" onfocus="onFocus(this)"><input type="text" maxlength="1" size="2" id="mSecond" at="2" onfocus="onFocus(this)"><input type="text" maxlength="1" size="2" id="mThird" at="3" onfocus="onFocus(this)"><input type="text" maxlength="1" size="2" id="mFourth" at="4" onfocus="onFocus(this)">
</li>
<script type="text/javascript">

window.onload = function() {
     document.getElementById("mFirst").focus(); 
}
var array = ["mFirst","mSecond","mThird","mFourth"];
function keyUp(e) {
  var curId = array[Number(e.getAttribute("at"))-1];
  var nextId = array[Number(e.getAttribute("at"))];
  var curval= e.value;
var letters = /^[0-9a-zA-Z]+$/;
if(e.value.match(letters)){
  document.getElementById(curId).value = curval;
  if(e.getAttribute("at") <= 3){
  var nextPos = document.getElementById(nextId).offsetLeft;
  e.setAttribute("at",Number(e.getAttribute("at"))+1);
  e.style.left = nextPos+"px";
  }
 e.value = ""
}else {
 e.value = "";
}
} 
function keyDown(e) {
    var curId = array[Number(e.getAttribute("at"))-1];
    document.getElementById(curId).value = "";
} 

function onFocus(e) {
  document.getElementById("hidebox").focus();
  document.getElementById("hidebox").setAttribute("at",Number(e.getAttribute("at")));
  document.getElementById("hidebox").style.left = e.offsetLeft+"px";
  document.getElementById("hidebox").style.top = e.offsetTop+"px";
}

</script>
</body>
</html>

暂无
暂无

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

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