mercredi 17 mars 2021

How to undo a javascript command after unchecking the checkbox?

I need to write a program to calculate the price of being at the garage, but I don't know how to cancel the command after I unchecking the checkbox. here's my code:

function Car() {
  var value = 0;
  var PrivateCar = document.getElementById("PrivateCar").value;
  var CommercialCar = document.getElementById("CommercialCar").value;
  var Truck = document.getElementById("Truck").value;
  var ScentTree = document.getElementById("ScentTree").value;
  var PrivateCarWax = document.getElementById("PrivateCarWax").value;
  var CommercialCarWax = document.getElementById("CommercialCarWax").value;
  if (document.form1.PrivateCar.checked == true) {
    document.getElementById("Car").innerHTML = "You have a private car." + "<br />";
    value += 30;
  }
  if (document.form1.CommercialCar.checked == true) {
    document.getElementById("Car").innerHTML = "You have a commercial car" + "<br />";
    value += 45;
  }
  if (document.form1.Truck.checked == true) {
    document.getElementById("Car").innerHTML = "You have a truck" + "<br />";
    value += 60;
  }
  if (document.form1.ScentTree.checked == true) {
    document.getElementById("Extras").innerHTML = "You have chosen the addition of a scent tree" + "<br />";
    value += 5;
  }
  if (document.form1.PrivateCarWax.checked == true) {
    document.getElementById("Extras").innerHTML = "You have chosen the addition of Wax to a private car" + "<br />";
    value += 10;
  }
  if (document.form1.CommercialCarWax.checked == true) {
    document.getElementById("Extras").innerHTML = "You have chosen the addition of Wax to a private car" + "<br />";
    value += 20;
  }
  document.getElementById("price").innerHTML = "The price you have to pay is: " + value + " dollars";
}
<body dir="rtl">
  <form name="form1">
    <h1 style="color:red">Car Washing Price List</h1>
    <br />
    <b>Car Type:</b>
    <br />
    <input type="radio" id="PrivateCar" name="CarType" /> Private Car - 30 dollars
    <br />
    <input type="radio" id="CommercialCar" name="CarType" /> Commercial Car - 45 dollars
    <br />
    <input type="radio" id="Truck" name="CarType" /> Truck - 60 dollars
    <br />
    <br />
    <b>Extras:</b>
    <br />
    <input type="checkbox" id="ScentTree" /> Scent tree - 5 dollars
    <br />
    <input type="checkbox" id="PrivateCarWax" /> Wax for private car - 10 dollars
    <br />
    <input type="checkbox" id="CommercialCarWax" /> Wax for commercial car - 20 dollars
    <br />
    <input type="checkbox" id="TruckWax" disabled /> Wax for truck - You can't choose wax for a truck
    <br />
    <br />
    <input type="button" id="button" value="Click to get a price" onclick="Car()" />
    <p id="Car"></p>
    <p id="Extras"></p>
    <p id="price"></p>
  </form>

I need to:

  1. Disable the "CommercialCarWax" checkbox after clicking "PrivateCar" radio button.
  2. Disable the "PrivateCarWax" checkbox after clicking "CommercialCar" radio button.
  3. Disable the "CommercialCarWax" checkbox and the "PrivateCarWax" checkbox after clicking "Truck" radio button.

Aucun commentaire:

Enregistrer un commentaire