keypress无法在移动浏览器上运行

时间:2017-02-20 15:14:16

标签: javascript meteor

此Meteor客户端事件在桌面浏览器上运行正常但在移动浏览器“Chrome”上无法执行相同操作。

它在“@”之后检测到“g”的键输入,并将其替换为“@ gmail.com” 知道如何让它在手机上工作吗? THX

Template.input.events({
  'keypress input': function (evt, template) {
    if (evt.which === 13) {
      //do stuff
    }
    else if (Session.get('taskSelected') === 'walk') {
      if (evt.which == 103) { // "g" has been typed do gmail.com
        utility.inputReplaceWith('gmail.com', evt);
      }
      else if (evt.which === 121) {  // "y" for yahoo.com
        utility.inputReplaceWith('yahoo.com', evt);
      }
      else if (evt.which === 104) {
        utility.inputReplaceWith('hotmail.com', evt);
      }
    }
  }
});

    inputReplaceWith: (text, evt) => {
      let elem = document.getElementsByName('email')[0].value;
      if (elem.slice(-1) == '@') { // last char is "@"
        evt.preventDefault();
        document.getElementsByName('email')[0].value = elem + text;
      }
    },

2 个答案:

答案 0 :(得分:1)

有一个textInput事件为您提供输入的字符,也可以取消

const inputField = document.getElementById('wanted-input-field');

inputField.addEventListener('textInput', function(e) {
    // e.data will be the 1:1 input you done
    const char = e.data; // In our example = "a"

    // If you want the keyCode..
    const keyCode = char.charCodeAt(0); // a = 97

    // Stop processing if "a" is pressed
    if (keyCode === 97) {
        e.preventDefault();
        return false;
    }
    return true;
});

答案 1 :(得分:0)

Chrome for Android中的"keypress"事件看起来很麻烦。

请参阅:

TL; DR:改为使用"keydown"和/或"keyup"个事件,甚至是"input"个事件。

相关问题