繁体   English   中英

为什么我的js函数不能在计算机上工作,而不能在手机上工作?

[英]Why isn't my js function working in computer but not in cellphone?

输入数字后,我必须自动聚焦下一个输入元素。

我的HTML:

          "<input id='num-1' class='inp-num' data-pos='0' type='text' maxlength='1' name='one' onkeypress='isInputNumber(event)' autofocus='autofocus'/>" +
          "<input id='num-2' class='inp-num' data-pos='1' type='text' maxlength='1' name='two' onkeypress='isInputNumber(event)'>" +
          "<input id='num-3' class='inp-num' data-pos='2' type='text' maxlength='1' name='three' onkeypress='isInputNumber(event)'>" +
          "<input id='num-4' class='inp-num' data-pos='3' type='text' maxlength='1' name='four' onkeypress='isInputNumber(event)'>" +

在我的JavaScript中,我有:

  $('.inp-num').on('keyup', function() {
    if ($(this).val()) {
       $(this).next().focus();
     }
  });

这实际上工作正常,事情是当我试图在手机上使用它时,它一直保持相同的输入,而不用转到下一个。 我真的需要帮助。

谢谢!

在触发keyup事件之前,手机的浏览器未在input元素内接收值。 因此, if ($(this).val())为false。 您需要使用其他事件类型(例如input )来避免这种情况。

更多信息和游乐场:

https://www.w3schools.com/jsref/event_onchange.asp

$('.inp-num').on('input', function() {
 if ($(this).val()) {
   $(this).next().focus();
 }
});

暂无
暂无

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

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