形成价格输入

时间:2015-12-16 17:22:19

标签: javascript actionscript-3

理想情况下这适用于AS3,但我总是可以将其从JavaScript转换下来。基本上我想将类型数字放入一个字段并让它添加小数。问题是,键盘不会有一个"。"键

所以如果最终输出是10.45

在我输入时会自动开始格式化:

1它将改为.01。而且我一直打字.. 0.01
0.10
10.40 10.45

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这可以通过一些简单的字符串操作来完成。只需将字符串拆分成部分并插入小数。

HTML

function formatPrice(obj) {
    //remove any existing decimal
    p = obj.value.replace('.','');

    //get everything except the last 2 digits
    var l = p.substring(-2, p.length-2);

    //get the last 2 digits
    var r = p.substring(p.length-2,p.length);

    //update the value
    obj.value = l + '.' + r;
}

的Javascript

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:CanExecute/>
   </soapenv:Body>
</soapenv:Envelope>

您可以在此处看到它:https://jsfiddle.net/x3wey5uk/