开发者

Auto Calculating for input field values

My Html is like this:

<input class="gbTr开发者_开发百科ansform" type="text" name="maxdl" value=""/>

And javascript like this:

$('.gbTransform').change(function(){
        var sz = $(this).val().match(/(\d+)gb/i);
        if(sz.length > 1){
          $(this).val(parseInt(sz[1])*1024);
        }
    });

What this does is when a user types 1gb it will automatically calculate into mbs (1 * 1024 = 1024) and change the input fields value.

This works fine but when user types 1.5gb its calculates 5 * 1024 which is incorrect it should be 1.5 * 1024

Thank You.


You can clear all "gb" strings and use parseFloat:

parseFloat("1.5"); // 1.5

Example:

var a = "1.5gb".replace("gb", "");
document.print(parseFloat(a));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜