简体   繁体   English

带收音机标签的简单JS计算器

[英]Simple js calculator with radio tag

I found subjects like mine but I could not fix my problem. 我发现了类似我的主题,但无法解决我的问题。 So I wanted to calculate the cost of tickets form radio tags by multiplying the price with the amount of tickets using if statements for each option without jquery. 因此,我想通过使用if语句为每个选项(不使用jquery)将价格乘以门票数量,从而计算单选标签的门票成本。 I can not figure it out why I do not have an output. 我不知道为什么我没有输出。 It looks like none of my functions work and I can not find why. 看起来我的所有功能都无法正常工作,我找不到原因。

Can you help me please, I know it is easy but I am still a beginner 你能帮我吗,我知道这很简单,但是我还是一个初学者

 <!doctype html> <html> <head> <title>No Boundaries</title> <link rel="stylesheet" href="styles1.css"> <script type="text/javascript"> function validate(){ if(!document.form1.cond.checked) {alert("Please accept the Terms and Conditions"); return false; } if(document.form1.name.value.length < 2) {alert(“Please enter your full name, not just an initial”); return false; } return true; } function cost() { if (document.form1.accom["v"].checked) { var amount = parseInt(document.form1.amount.value); var ans = 90 * amount; document.form1.total1.value = ans; } else if (document.form1.accom["t"].checked) { var amount = parseInt(document.form1.amount.value); var ans = 150 * amount; document.form1.total1.value = ans; } else if (document.form1.accom["c"].checked) { var amount = parseInt(document.form1.amount.value); var ans = 45 * amount; document.form1.total1.value = ans; } } function post(){ if(document.form1.del["1"].checked){ var num = 0; var ans = num; document.form1.total2.value = ans; } if(document.form1.del["2"].checked){ var num = 12; var ans = num; document.form1.total2.value = ans; } if(document.form1.del["3"].checked){ var num = 16; var ans = num; document.form1.total2.value = ans; } if(document.form1.del["4"].checked){ var num = 20; var ans = num; document.form1.total2.value = ans; } } function damage(){ var total1 = parseInt(document.form1.total1.value); var total1 = parseInt(document.form1.total1.value); var ans = total1 + total2; document.form.total3.value = ans; } </script> </head> <body> <section> <form name="form1"> <fieldset> <legend>Personal details</legend> <div> Please enter your full name<br> <input name="name" type="text" required onsubmit="return validate();"><br> </div> <div> Address<br> <input name="addresss" type="text" required><br> <input type="text"><br> <input type="text"><br> <input type="text"><br> </div> <div> Phone Number<br> <input name="phone" type="tel"><br> </div> <div> Email Address<br> <input name="email" type="email" required><br> </div> <div> Date of Birth<br> <input name="birth" type="date"><br> </div> </fieldset> <fieldset> <legend>Ticket Details</legend> <div> Type of T<br> <label for="vil">V</label> <input type="radio" name="accom[]" value="v"> <label for="town">T</label> <input type="radio" name="accom[]" value="t"> <label for="con">C</label> <input type="radio" name="accom[]" value="c"> </div> <div> Quantity of T <input name="amount" type="number" min="1" max="10" required><br> <br> <input type="button" name="cost" value="CC" onclick="cost();"><br> </div> <div> Delivery Options<br> <input type="radio" name="del[]" value="1" >Free<br> <input type="radio" name="del[]" value="2" >£12<br> <input type="radio" name="del[]" value="3" >£16<br> <input type="radio" name="del[]" value="4" >£20<br> <br> <input type="button" value="CD" onclick="post();"><br> </div> </fieldset> <fieldset> <legend>Terms And Conditions</legend> <input name="cond" type="checkbox" onsubmit="return validate();"> </fieldset> <fieldset> <legend>Cost</legend> <input type="text" name="total1" readonly><br> <input type="text" name="total2" readonly><br> <input type="text" name="total3" readonly><br> <br> <input type="button" value="CFC" onclick="damage();"><br> </fieldset> <br><br> <fieldset> <input type="submit"> </fieldset> </form> </section> </body> </html> 

the cost function should be something like this, 成本函数应该是这样的,

function cost() {
    if (document.form1.accom.value.toLowerCase() == "v") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 90 * amount;
        document.form1.total1.value = ans;
    } else if (document.form1.accom.value.toLowerCase() == "t") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 150 * amount;
        document.form1.total1.value = ans;
    } else if (document.form1.accom.value.toLowerCase() == "c") {
        var amount = parseInt(document.form1.amount.value);
        var ans = 45 * amount;
        document.form1.total1.value = ans;
    }
}

And to make the code more refactored, make it like this, 为了使代码更具重构性,请使其如下所示,

function cost() {
    var val = document.form1.accom.value.toLowerCase();
    var amount = parseInt(document.form1.amount.value);
    var ans;
    if (val == "v") {
        ans = 90 * amount;
    } else if (val == "t") {
        ans = 150 * amount;
    } else if (val == "c") {
        ans = 45 * amount;
    }
    document.form1.total1.value = ans;
}

Ok I found your errors. 好的,我发现了您的错误。 This is the code for html 这是html的代码

<div>
   Type of T<br>
   <label for="vil">V</label>
   <input type="radio" name="accom" value="v">
   <label for="town">T</label>
   <input type="radio" name="accom" value="t">
    <label for="con">C</label>
</div>
<div>
   Quantity of T
   <input name="amount" type="number" min="1" max="10" required><br>
   <br>
   <input type="button" name="cost" value="C C" onclick="costs();"><br>
 </div>

Next, in your javascript you have tho change the name of function; 接下来,您可以在javascript中更改函数的名称; instead of cost you have to renaming in costs 而不是成本,您必须重命名成本

 function costs() {
    if (document.form1.accom.value == 'v') {
        var amount = parseInt(document.form1.amount.value);
        var ans = 90 * amount;
        document.form1.total1.value = ans;
    } 
    else if (document.form1.accom.value == 't') {
        var amount = parseInt(document.form1.amount.value);
        var ans = 150 * amount;
        document.form1.total1.value = ans;
    } 
    else if (document.form1.a`enter code here`ccom.value == 'c') {
        var amount = parseInt(document.form1.amount.value);
        var ans = 45 * amount;
        document.form1.total1.value = ans;
    }
}

That's all 就这样

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

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