简体   繁体   English

带单选按钮的简单JS计算器

[英]Simple JS calculator with radio buttons

I found subjects like mine but I couldn't fix my problem after reading them, So I wanted to make a simple calculator using JS but I couldn't do it and it keeps doing nothing ! 我发现了像我这样的主题,但在阅读它们后无法解决问题,因此我想使用JS制作一个简单的计算器,但我却做不到,而且它什么也没做! I think the main problem is with the radio buttons. 我认为主要问题在于单选按钮。

Can you help me, it's kind of easy I know but I'm still a beginner. 您能帮我吗,我知道这很容易,但我仍然是一个初学者。 PS: I don't want jquery!!! PS:我不要jQuery !!!

<!DOCTYPE html>
<html>
<head>
    <title>Exercice1 | JS</title>
    <script>
        function calcul (op, v1, v2){
                if (document.f.op[1].cheked) alert(parseInt(v1.value) + parseInt(v2.value));
                if (document.f.op[2].cheked) alert(parseInt(v1.value) - parseInt(v2.value));
                if (document.f.op[3].cheked) alert(parseInt(v1.value) * parseInt(v2.value));
                if (document.f.op[4].cheked) alert(parseInt(v1.value) / parseInt(v2.value));
            }   
    </script>
</head>
<body>
    <form name ="f" method="POST" action="">
        <label for="v1">Var 1</label>
        <input type="text" name="v1" id="v1"></input>
        <br> <br>
        <label for="v2">Var 2</label>
        <input type="text" name="v2" id="v2"></input>
        <br><br>

        <input type="radio" name="op" value="1"></input>
        <label for="1">+</label>
        <br>
        <input type="radio" name="op" value="2"></input>
        <label for="2">-</label>
        <br>
        <input type="radio" name="op" value="3"></input>
        <label for="3">*</label>
        <br>
        <input type="radio" name="op" value="4"></input>
        <label for="4">/</label>
        <br>
        <input type="submit" onclick="calcul(op, v1, v2)" value="Calculate"></input>
        <input type="reset"></input>
    </form>
</body>

Few things that could fix off my head: 几乎没有什么可以解决我的问题的:

Do not pass op, v1, v2 in your calcul function, and change your function to function calcul () because you've used those three - (op, v1, v2) to declare your elements' name's anyways - variables with the same names are accessible in your function anyways 请勿在calcul函数中传递op,v1,v2,并将函数更改为function calcul(),因为您已经使用了这三个-(op,v1,v2)来声明元素的名称-变量名称相同无论如何都可以在您的功能中访问

cheked is incorrect, it's .checked 咀嚼不正确,已检查

    function calcul (){
                    if (document.f.op[0].checked) alert(parseInt(v1.value) + parseInt(v2.value)); 
// you are accessing an array - so index starts with Zero
                    if (document.f.op[1].checked) alert(parseInt(v1.value) - parseInt(v2.value));
                    if (document.f.op[2].checked) alert(parseInt(v1.value) * parseInt(v2.value));
                    if (document.f.op[3].checked) alert(parseInt(v1.value) / parseInt(v2.value));
                }   

Change this too: 也更改此:

onclick="calcul()" 

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

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