html 中输入的最大长度属性在 HTC One M7 上不起作用

新手上路,请多包涵

我有一个简单的输入字段,它有一个 maxlength=“2” 属性。代码如下所示:

 <input id="txtLoginName" maxlength="2">

它在大多数 Android 设备上运行良好。但是,在 HTC One M7 上,它不起作用。在这个设备上,它只允许我输入任意数量的字符。

有什么建议吗?到目前为止,我认为这应该是一个特定于设备的问题。

提前致谢。

原文由 Long Dao 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 165
1 个回答

试试这个:

查询:

 var $input = $('input')
$input.keyup(function(e) {
    var max = 5;
    if ($input.val().length > max) {
      $input.val($input.val().substr(0, max));
    }
});

香草 js:

 const input = document.getElementById('address1');
const max = Number(input.getAttribute('maxlength'));

input.addEventListener('keyup', function() {
    if (this.value.length > max) {
      this.value = this.value.substr(0, max)
    }
});

原文由 Kevin Sebastian Fernandez 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题