首页 > 解决方案 > 带有初始值的错误十进制数值(Inputmask JQuery)

问题描述

我的初始化值有问题。初始值2042.09应该是2.042,09,但它给出204.209,00

$(".target").inputmask('numeric', {
            radixPoint:",",
            groupSeparator: ".",
            allowMinus: false,
            unmaskAsNumber: true,
            digits: 2,
            removeMaskOnSubmit: true,

            autoUnmask: true,
            digitsOptional: false,
        })

标签: javascriptjqueryinput-maskjquery-inputmask

解决方案


文档说明了有关设置初始值的以下内容:

//html
<input name="npt" value="123456,789"/>

//js
Inputmask("decimal", {
    radixPoint: ',',
    inputtype: "text"
}).mask("input");

$("input").val("123456,789");
$("input").val(123456.789); //this doesn't work because jQuery converts the number to a string before passing it along to the Inputmask.

document.getElementsByName("npt")[0].value = "123456,789";
document.getElementsByName("npt")[0].value = 123456.789; //type number

即在 HTML 中或通过 jQuery 声明初始值时,您需要尊重语法.val()


推荐阅读