Android Chrome输入onkeydown [英] Android Chrome input onkeydown

查看:113
本文介绍了Android Chrome输入onkeydown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这个减少到最小代码来显示这个问题。问题是,在我的电脑上,这在Chrome上运行良好,但在我的Android上的Chrome上,它无法正常工作。首先,高度简化的代码:

 < html>< head>< script language ='javascript'> 
函数kd(e){
inp = document.getElementById('myinput');
if(inp.value =='example')inp.value ='';
返回true;
}
< / head>< body>
< input type ='text'id ='myinput'value ='example'
onkeydown ='return kd(event);'>
< / body>< / html>

在我的计算机上,我将光标放在说明示例的框中。我按任意键。 example这个词消失了,我按下的任何字母都会出现。



现在,在Android / Chrome上,我将光标放在示例中的框中。我按任意键。 例子这个词消失了,但是我按下的那个字母没有出现。如果我按另一个键,它会显示该字母。我可以继续打字。它只是拒绝显示我按下的第一个字母。



这显然与值有关=''部分的JavaScript。如果我注释掉该行,它可以正常工作(但按下第一个键时不会擦除example一词)。



更新:我在苹果手机。它正确显示了第一个按键的字母。所以,这仅限于Android或Android的Chrome。

  $('input')。val(''); 

以及使用 keyup 事件的更佳做法因为用户倾向于在键盘上拖动他们的手指,所以你要确保你得到了他们想要的东西

I have reduced this to the minimum code to show this problem. The problem is that this works fine on Chrome on my computer, but on Chrome on my Android, it doesn't work properly. First, the highly simplified code:

<html><head><script language='javascript'>
function kd(e) {
  inp = document.getElementById('myinput');
  if(inp.value=='example') inp.value='';
  return true;
}
</head><body>
  <input type='text' id='myinput' value='example'
  onkeydown='return kd(event);'>
</body></html>

On my computer, I put the cursor in the box that says example. I press any key. The word "example" goes away and whatever letter I pressed appears.

Now, on Android/Chrome, I put the cursor in the box that says example. I press any key. The word "example" goes away, but the letter I pressed does not appear. If I press another key, it will show that letter. I can keep typing. It simply refuses to show the first letter I press.

This apparently has to do with the value='' part of the javascript. If I comment out that line, it works fine (but doesn't erase the word "example" when pressing the first key).

Update: I tested this on an iPhone. It shows the letter of the first keypress properly. So, this is limited to either Android or Android's Chrome.

解决方案

Clear the text on input focus

$('input').val('');

and its better practice to use keyup event since users tend to drag their fingers on the keyboard you want make sure you get what they intended

这篇关于Android Chrome输入onkeydown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆