简体   繁体   English

从javascript上的输入类型号中删除逗号

[英]Remove comma from input type number on javascript

now im doing PHP project combine with JQuery.现在我正在做 PHP 项目与 JQuery 结合。 i make a form page that have <input type="number">我制作了一个具有<input type="number">的表单页面

From this input, i make a rules for limit 2 decimal behind.根据这个输入,我制定了限制小数点后 2 位的规则。 here is the code这是代码

<input 
type = "number"
onkeypress = "return isNumeric(event)"
oninput = "maxLengthCheck(this)"
name ="cost"> 

JS JS

function maxLengthCheck(object) {
    if (object.value.length > object.maxLength)
      object.value = object.value.slice(0, object.maxLength)

    var prev = object.getAttribute("data-prev");
    prev = (prev != '') ? prev : '';
    if (Math.round(object.value*100)/100!=object.value)
    object.value=prev;
    object.setAttribute("data-prev",object.value)
}

function isNumeric (evt) {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode (key);
    var regex = /[0-9]|\./;
    if ( !regex.test(key) ) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();
    }
}

the code can work correctly.代码可以正常工作。 but however when insert more then 2 decimal behind the (.)dot.但是当在 (.) 点后面插入超过 2 位小数时。 the (.)dot will change into (,)comma. (.) 点会变成 (,) 逗号。

ex.前任。 "19.459". “19.459”。 on input will display "19,45".输入时将显示“19,45”。 this is wrong这是错误的

the correct is ex.正确的是前。 input "19.878" i want to display "19.87"输入“19.878”我想显示“19.87”

i make a sample on this site https://repl.it/@ferdinandgush/add-2-decimal-behind .我在这个网站上做了一个样本https://repl.it/@ferdinandgush/add-2-decimal-behind just click "run" buttom on the top and you able to test it只需单击顶部的“运行”按钮即可进行测试

how do i still able to keep the (.)dot evendo i type another number behind (.)dot ?我如何仍然能够保持 (.) 点即使我在 (.) 点后面键入另一个数字?

You are probably in a country that normally uses a comma as the decimal separator and browsers use that for <input type=number> display.您可能所在的国家/地区通常使用逗号作为小数点分隔符,而浏览器将其用于<input type=number>显示。

Some browsers, such as Firefox, use the lang attribute to determine the language to use, when it's set on the input or any of its parent elements, so you could do that.某些浏览器(例如 Firefox)使用lang属性来确定要使用的语言,当它在输入或其任何父元素上设置时,因此您可以这样做。

However other browsers such as Chrome do not respect that lang attribute.然而,其他浏览器(如 Chrome)不尊重该lang属性。 If you need this to work in all browsers, then your only choice is, as far as I can say, not to use <input type=number> .如果你需要它在所有浏览器中工作,那么你唯一的选择是,据我所知,不使用<input type=number>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM