简体   繁体   English

在不使用.blur()的Javascript / AngularJS中关闭Safari中的IOS键盘

[英]Close IOS keyboard in Safari with Javascript/AngularJS without .blur()

My problem: I need to close the IOS keyboard when an HTML form is submitted using the return button in Safari mobile. 我的问题:使用Safari移动版中的返回按钮提交HTML表单时,我需要关闭IOS键盘。 All solutions I can find for this problem say that using element.blur() should close the keyboard, so I made an Angular directive to do just that. 我可以找到的所有解决方案都说使用element.blur()应该关闭键盘,因此我做了一个Angular指令来做到这一点。 However, it doesn't seem to work unless Done is pressed instead. 但是,除非按Done,否则它似乎不起作用。 Some examples of what I've tried: 我尝试过的一些示例:

form.on('submit', function () {
  var textfields = form.find('input');
  textfields[0].blur();
});



form.on('submit', function () {
  var textfields = form.find('input');
  textfields[0].focus();
  textfields[0].blur();
})



var defocusElement = angular.element('<input style="opacity: 0; width: 0" type="button">');

form.append(defocusElement);

form.on('submit', function () {
  defocusElement.focus();
})

Could someone recommend another avenue I can try? 有人可以推荐我可以尝试的另一条途径吗? All these solutions work fine for Android. 所有这些解决方案都适用于Android。

Check for an onKeyUp event with keyCode 13(return key) and close the event. 使用keyCode 13(返回键)检查onKeyUp事件并关闭该事件。

<input type="text" onkeyup="closeKeyboard(event)">

closeKeyboard: closeKeyboard:

var closeKeyboard = function(event){
    if(event.keyCode == 13)
         //code to close the keyboard
}

This will work if for some reason element.blur() is not working: 如果由于某种原因element.blur()不起作用,这将起作用:

function directive(_) {
   var directive = {
      restrict: 'A',
      link: function (scope, element) {
        var defocusElement = angular.element('<input style="opacity: 0; width: 0" type="button">');

        element.append(defocusElement);

        // Close keyboard on submit.
        element.on('submit', function () {
          defocusElement.focus();
        });

        // Handles return key on IOS Safari keyboard.
        element.on('keyup', function (event) {
          if(event.keyCode === 13) {
            defocusElement.focus();
          }
        });
      }
    };

    return directive;
  }

Thanks ayushgp! 谢谢ayushgp!

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

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