去掉 0 和任何非数字字符 Javascript

发布于 2024-09-25 16:51:38 字数 425 浏览 7 评论 0原文

我如何更改此代码以不允许 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>

How could I change this code to not allow a 0 and to strip out all non number characters?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2024-10-02 16:51:38

编辑:更新的答案:

您只需删除所有非非数字,然后测试该数字是否不是 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

EDIT: updated answer:

you simply strip all the non non numbers then test if the number is not a 0 THEN you can perform your function.

   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);
    };

Hope this helps. Thanks
Let me know if it does.

PK

盗梦空间 2024-10-02 16:51:38
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);
};
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);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文