2017-02-03 2 views
0

Wie würde ich eine Kosten für die Kontrollkästchen hinzufügen. Wenn ich ein onClick hinzufüge, sagte es myFunction wurde nicht definiert. Ich weiß nicht, was ich tue. Ich habe versucht, eine Überprüfung mit der zweiten Funktion, also wenn es überprüft wurde, würde es die Kosten zusammen und eine Zwischensumme hinzufügen, aber ich kann es nicht funktionieren oder einen Weg finden, um die Kosten zu einem Wert in den KontrollkästchenJavascript - Wie addiere ich Kosten?

<html> 
    <body> 
     <p>A pizza is 13 dollars with no toppings.</p> 
     <form action="form_action.asp"> 
     <input type="checkbox" name="pizza" value="Pepperoni" id="pep" > Pepperoni + 5$<br> 
     <input type="checkbox" name="pizza" value="Cheese" id="ch" >Cheese + 4$<br> 
     <br> 
     <input type="button" onClick="myFunction()" value="Send order"><br> 
     <input type="button" onClick="cost()" value="Get cost" > <br> 
     <input type="text" id="order" size="50"> 
     </form> 
     <script type="text/javascript"> 
     function myFunction() { 
      var pizza = document.forms[0]; 
      var txt = ""; 
      var i; 
      for (i = 0; i < pizza.length; i++) { 
       if (pizza[i].checked) { // this shows the you ordered the pizza with blank topping 
        txt = txt + pizza[i].value + " "; 
       } 
      } 
      document.getElementById("order").value = "You ordered a pizza with: " + txt; 
     } 


     function cost() { 
      var x = document.getElementById(pep).checked; // this is the failed check and i dont know how to fix it and get it to add a cost 
      document.getElementById("demo").innerhtml = x; 
     } 
     </script> 
     <p id="demo"></p> 
    </body> 
</html> 
+0

Versuchen Sie, Ihre Funktion vor dem Formular-Tag zu definieren. – Shubham

Antwort

0

Antwort, dass https://stackoverflow.com/users/7488236/manish-poduval bekam

<html> 
<body> 

<p>A pizza is 13 dollars with no toppings.</p> 

<form action="form_action.asp"> 
<input type="checkbox" name="pizza" value="Pepperoni" id="pep">Pepperoni + 5$<br> 
<input type="checkbox" name="pizza" value="Cheese" id="che">Cheese + 4$<br> 
<br> 
<input type="button" onclick="myFunction()" value="Send order"> 
<input type="button" onclick="cost()" value="Get cost"> 
<br><br> 
<input type="text" id="order" size="50"> 
</form> 

<script> 
function myFunction() { 
var pizza = document.forms[0]; 
var txt = ""; 
var i; 
for (i = 0; i < pizza.length; i++) { 
    if (pizza[i].checked) { 
     txt = txt + pizza[i].value + " "; 
    } 
} 
document.getElementById("order").value = "You ordered a pizza with: " + txt; 
} 

function cost() { 
var pep = 5; 
var che = 4; 
var pizza = 13; 
var total = 0; 
if (document.getElementById("pep").checked === true) { 
    total += pep; 
} 

if (document.getElementById("che").checked === true) { 
    total += che; 
} 
    document.getElementById("order").value = "The cost is : " + total; 
} 

</script> 

</body> 
</html> 
0
document.getElementById("pep").checked; 

Sie haben diese Zeile nicht korrekt geschrieben. Überprüfen Sie danach den Wert von x, wenn es wahr ist, dann addieren Sie den Wert 5 $ zu 13 $ und addieren Sie die Kosten und zeigen Sie sie in einem Textfeld an.

Lassen Sie mich wissen, ob es funktioniert oder nicht.