不确定如何显示结果// javascript

时间:2017-04-04 13:02:50

标签: javascript

好的,所以我的代码如下。单击计算价格按钮后,我希望它显示按钮下方的结果。即。汽车数量,汽车类型和汽车价格。我知道这可能非常容易,但我无法让它发挥作用。

HTML:

<!DOCTYPE html>
<html>
<body>
    <form name="Cars">
        <h1>Car Sales</h1>
        <p>Which type of car would you like (A, B or C)</p>
        <input type="text" name="CarType"><br>
        <p>how many cars would you like (1-100)</p>
        <input type="text" name="CarNumber"><br>
        <br>
        <button onclick="return beginfunction()">Calculate Price</button>
        <p id="message"></p>
        <script src="car.js">   </script>
    </form>
</body>

JavaScript的:

function beginfunction() {
  var CarType = document.forms["Cars"]["CarType"].value;
  var CarNumber = document.forms["Cars"]["CarNumber"].value;
  var CarPrice;
  if ( !( CarType == 'A' || CarType == 'B' || CarType == 'C' ) ) {
    CarTypeError = "Invalid Car Type";
    document.getElementById("message").innerHTML = CarTypeError;
    return false;

  }


  {
      if (isNaN(CarNumber)) {
        CarNumberError = "Invalid Quantity Entered";
        document.getElementById('message').innerHTML = CarNumberError;
          return false;
      }

  }
{
  if (CarNumber >0 && CarNumber <10)
  {

  }
  else
  CarError = "Invalid";
  document.getElementById('message').innerHTML = CarError;
  return false;
}
{
if (CarType == 'A') {
    CarPrice = 30;
} else if (CarType == 'B') {
    CarPrice = 20;
}  else if (CarType == 'C'){
  CarPrice = 10;
}
}

}

1 个答案:

答案 0 :(得分:0)

您正在计算CarPrice,但您不使用该变量。 您可以将其设置为您设置错误消息的<p>标记。 这样的事情。

if (CarType == 'A') {
    CarPrice = 30;
} else if (CarType == 'B') {
    CarPrice = 20;
}  else if (CarType == 'C'){
  CarPrice = 10;
}
document.getElementById('message').innerHTML = CarPrice;