자바스크립트 (JavaScript)에서 input 요소 (Element)에 숫자 포맷팅하기 (Number Formatting)
자바스크립트/공부 2014. 10. 26. 20:42
자바스크립트에서 input 요소에 숫자 포맷팅하기 위해
다음과 같이 jQuery NumBox 라이브러리 (Library)를 사용할 수 있다.
<script src="../../public/libs/jquery.numbox/1.1.1/jquery.numbox-1.1.1.min.js"
th:src="@{/libs/jquery.numbox/1.1.1/jquery.numbox-1.1.1.min.js}"></script>
<link rel="stylesheet"
href="../../public/libs/jquery.numbox/1.1.1/jquery.numbox-1.1.1.css"
th:href="@{/libs/jquery.numbox/1.1.1/jquery.numbox-1.1.1.css}" />
<script>
$(function () {
$('#currentSalary').NumBox({places: 0, symbol: '', max: 100000000});
});
</script>
<input id="currentSalary" name="currentSalary" type="number" class="form-control" />
안타깝게도 크롬 (Chrome)에서 다음과 같은 에러가 발생한다.
Uncaught InvalidStateError: Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection.
5Uncaught InvalidStateError: Failed to read the 'selectionEnd' property from 'HTMLInputElement': The input element's type ('number') does not support selection.
다행히도 동작에는 문제가 없다.
Stable 버전인 1.1.0은 파이어폭스 (Firefox)에서 동작하지 않는다.
파이어폭스에도 사용될 예정이라면 Hotfix 버전인 1.1.1을 사용해야만 한다.
-----
모바일 크롬에서 입력을 위해 입력창을 두번 터치해야되는 문제도 있다.
Reference:
댓글을 달아 주세요