html 中输入的 maxlength 属性在 HTC One M7 上不起作用

2024-02-13

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

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

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

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

提前致谢。


试试这个:

jQuery:

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)
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

html 中输入的 maxlength 属性在 HTC One M7 上不起作用 的相关文章

随机推荐