美文网首页
js input监听事件

js input监听事件

作者: 曹锦花 | 来源:发表于2019-08-08 13:51 被阅读0次
<!Doctype html>
<html>
<head>
</head>
<body>
    <input id="1" type="number" />
    <script>
        var input = document.getElementById('1');

        input.addEventListener('input', (e) => {
            var s = e.srcElement.value;
            if (s.split('.').length > 2) {
                s = s.split('.')[0] + '.' + s.split('.')[1];
            }
            if (s.split('.')[1]) {
                s = parseInt(s.split('.')[0].substr(0, 5)) + '.' + s.split('.')[1].substr(0, 2);
                
            } else {
                s = parseInt(s).toString();
            }
            if (s.split('.')[0].length > 5) {
                s = parseInt(s.split('.')[0].substr(0, 5)).toString();
            }
            
            input.value = s;
        });
    </script>
</body>
</html>

相关文章

网友评论

      本文标题:js input监听事件

      本文链接:https://www.haomeiwen.com/subject/rwthjctx.html