input type = "number"소수점 뒤에 두 자리 유지

10575 단어 프런트 엔드
사실 아래의 방법도 low로 보이지만 많은 자료를 찾았지만 완벽한 방법을 찾지 못했다.
<input type="number" class="number">

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js">script>
<script type="text/javascript">
$(function() {
    $('.number').change(function() {
        let val = parseFloat($(this).val());
        if (isNaN(val)) {
            val = 0;
        }
        $(this).val(val.toFixed(2));
    });
});
script>

관련: Bootstrap TouchSpin


input type = "number"제어 소수점 비트 관련 자료를 볼 때 관련 플러그인이 괜찮습니다.
<input type="number" class="number">

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js">script>
<script type="text/javascript" src="jquery.bootstrap-touchspin.js">script>
<script type="text/javascript">
$(function() {
    $(".number").TouchSpin({
        min: 0,
        max: 100,
        step: 0.1,
        decimals: 2,
        boostat: 5,
        maxboostedstep: 10,
        postfix: '%'
    });
});
script>

Settings:
OPTION
DEFAULT
DESCRIPTION
initval
“”
Applied when no explicit value is set on the input with the value attribute. Empty string means that the value remains empty on initialization.
min
0
Minimum value.
max
백
Maximum value.
step
일
Incremental/decremental step on up/down change.
forcestepdivisibility
‘round’
How to force the value to be divisible by step value: ‘none’
decimals
0
Number of decimal points.
stepinterval
백
Refresh rate of the spinner in milliseconds.
stepintervaldelay
500
Time in milliseconds before the spinner starts to spin.
verticalbuttons
false
Enables the traditional up/down buttons.
verticalupclass
‘glyphicon glyphicon-chevron-up’
Class of the up button with vertical buttons mode enabled.
verticaldownclass
‘glyphicon glyphicon-chevron-down’
Class of the down button with vertical buttons mode enabled.
prefix
“”
Text before the input.
postfix
“”
Text after the input.
prefix_extraclass
“”
Extra class(es) for prefix.
postfix_extraclass
“”
Extra class(es) for postfix.
booster
true
If enabled, the the spinner is continually becoming faster as holding the button.
boostat
십
Boost at every nth step.
maxboostedstep
false
Maximum step when boosted.
mousewheel
true
Enables the mouse wheel to change the value of the input.
buttondown_class
‘btn btn-default’
Class(es) of down button.
buttonup_class
‘btn btn-default’
Class(es) of up button.

좋은 웹페이지 즐겨찾기