我想防止 0 作为第一个字符。但是当我再次 focusout 和 focusin 时,然后我在第一个中添加“2”,它仍然运行,我想要不应该运行。
这是我的 HTML
<input type="number" id="test">
我的JS
$('#test').keypress(function(evt) {
if (evt.which == "0".charCodeAt(0) && $(this).val().trim() == "") {
return false;
}
});
有人帮助或建议我该怎么做吗?谢谢你。
原文由 dedi wibisono 发布,翻译遵循 CC BY-SA 4.0 许可协议
You can use
input
event, which also handles pasted values,String.prototype.replace()
withRegExp
/^0/
to replace all0
在元素的.value
中找到的字符