我有一个简单的输入字段,它有一个 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));
}
});
Vanilla 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)
}
});
关于android - html 中输入的最大长度属性在 HTC One M7 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443007/