如何更改此代码以禁止0并删除所有非数字字符?
<script type="text/javascript">
(function() {
var a= document.getElementsByName('a')[0];
var b= document.getElementsByName('b')[0];
var c= document.getElementsByName('c')[0];
a.onchange=b.onchange=a.onkeyup=b.onkeyup= function() {
c.value= Math.ceil((a.value/b.value)*100);
};
})();
</script>
答案 0 :(得分:2)
编辑:更新回答:
你只需删除所有非非数字,然后测试数字是否为0那么你可以执行你的功能。
a.onchange=b.onchange=a.onkeyup=b.onkeyup= function() {
// 1. First we will remove all non numbers from the values inputted by the user.
var aValue = new String(a.value);
var bValue = new String(b.value);
//Use regular expressions to strip out the non numbers incase the user types in non numbers.
aValue = aValue.replace(/[^0-9]/g, '');
bValue = bValue.replace(/[^0-9]/g, '');
float newAValue = parseFloat("aValue");
float newBValue = parseFloat("bValue");
//2. Then test to see if the user has typed 0 as the value if they haven't then you an perform the maths.
if((newAValue != 0) && (newBValue != 0))
c.value= Math.ceil((av/bv)*100);
};
希望这会有所帮助。谢谢 如果有,请告诉我。
<强> PK 强>
答案 1 :(得分:1)
a.onchange=b.onchange=a.onkeyup=b.onkeyup= function() {
var av = parseFloat(a.value), bv = parseFloat(b.value);
if(bv != 0)
c.value= Math.ceil((av/bv)*100);
};