【问题标题】:Jquery on input from touchscreen find current input从触摸屏输入的Jquery查找当前输入
【发布时间】:2020-01-12 16:24:08
【问题描述】:

我有用户 keypress 和 keydown 来按下当前键,但它在触摸屏上不起作用。

我认为我需要使用 .on('input'),但我如何获得该输入?

换句话说。我需要这个才能在触摸屏上工作:

$('#newtags').keypress(function(e){ 
  if (e.which==13 || e.which==32 || e.which==188){ 
    Do something
  }
});

更新: 谢谢你的输入。我使用了检查输入字段最后一个字符的方法,并在函数运行时删除最后一个字符:

$('#newtags').on('keyup', function(e){
  var valueofinput= $(this).val();
  var lastcharofinput = valueofinput.substr(valueofinput.length - 1);

  if (e.which==13 || e.which==32 || e.which==188 || e.which==190 || lastcharofinput==' ' || lastcharofinput==',' || lastcharofinput=='.'){
    if (lastcharofinput==' ' || lastcharofinput==',' || lastcharofinput=='.'){
      valueofinput = valueofinput.substr(0, valueofinput.length - 1);
    }
  }
});

【问题讨论】:

  • 您能否提供代码示例以帮助其他人理解您的问题?
  • $('#newtags').keypress(function(e){ if (e.which==13 || e.which==32 || e.which==188){
  • 您也可以查看值并查看最后一个字符,然后寻找" ""-"。触摸屏设备使用虚拟键盘,因此某些相同的事件不会触发。您可以考虑使用一种移动框架,它可以像桌面浏览器一样帮助捕获这些事件。

标签: jquery input


【解决方案1】:

与您的问题类似,可以查看以下内容:keyup, keydown and keypress events not working on mobile

$('#newtags').on('keydown', function(e){ 
  switch(e.which){
    case 13:
      console.log("Enter Pressed.");
      break;
    case 32:
      console.log("Space Pressed.");
      break;
    case 188:
      console.log("Dash Pressed.");
      break;
  }
});

您也可以使用if()switch() 可以用于多种场景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多