javascript - HTML 代码中的错误

标签 javascript html

我正在编写一个网页。我已完成所需的一切,但是,当您输入超过 30 的任何数量时,id = "shipping" 颜色将变为红色。它可以这样做,但对于小于 30 的任何东西也可以这样做。另外,我在将提交按钮发送到服务器/网址时遇到问题。任何帮助表示赞赏!我将附上我的代码!

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title> Widgets R' Us </title>

  <style>
    table,
    td {
      border: 1px solid black;
    }
  </style>

  <script type="text/javascript">
    //Check if non -number or a negative number
    function realNumber() {

      var qty1 = document.getElementById("Quantity1").value;
      var qty2 = document.getElementById("Quantity2").value;
      var qty3 = document.getElementById("Quantity3").value;

      //isNaN is a predetermined function
      if ((isNaN(qty1) || qty1 < 0) || (isNaN(qty2) || qty2 < 0) || (isNaN(qty3) || qty3 < 0)) {

        alert("The quantity given is not a real number or is a negative number!");
        return false; //Will not allow for data to go to server.	
      } else {
        return true;
      }
    }

    function total() {

      var p1 = document.getElementById("Price1").value; //Value is referenced to the input tag
      var p2 = document.getElementById("Price2").value;
      var p3 = document.getElementById("Price3").value;
      var qty1 = document.getElementById("Quantity1").value;
      var qty2 = document.getElementById("Quantity2").value;
      var qty3 = document.getElementById("Quantity3").value;
      var over = qty1 + qty2 + qty3;

      if (realNumber()) {
        var totals = (p1 * qty1) + (p2 * qty2) + (p3 * qty3);
        var yes = (totals).toFixed(2);
        document.getElementById("ProductTotal").innerHTML = "$" + yes;

        if (over > 30) {
          document.getElementById("shipping").style.color = "red";

        } else {
          document.getElementById("shipping").style.color = "black";
        }
      }
    }

    function checkOut() {

      if (total()) {
        if ((document.getElementById("shipping").style.color == "red") &&
          (document.getElementById("extra").checked)) {

          return true;
        }
      }
      return false;

    }
  </script>
</head>

<body>

  <div>

    <h1><b><big> Widgets R' Us </b></strong>
    </h1>

    <h2><b> Order/Checkout </b></h2>

    <form name "widgets" onsubmit="return checkOut();" action="https://www.reddit.com/" method="get">

      <div id="mainTable">

        <table>
          <tr>
            <td>Widget Model:</td>
            <td>37AX-L
              <td>
          </tr>
          <tr>
            <td>Price per Unit:</td>
            <td>$12.45 <input type="hidden" id="Price1" name="Price1" value="12.45" /</td>
          </tr>
          <tr>
            <td>State:</td>
            <td>Regular</td>
          </tr>
          <tr>
            <td>Quantity:</td>
            <td> <input type="text" id="Quantity1" name="Quantity1" value="0" /</td>
          </tr>
        </table>

        <tr>
          <td> &nbsp;</td>
          <td>&nbsp;</td>
        </tr>

        <table>
          <tr>
            <td>Widget Model:</td>
            <td>42XR-J</td>
          </tr>
          <tr>
            <td>Price per Unit:</td>
            <td>$15.34 <input type="hidden" id="Price2" name="Price2" value="15.34" /></td>
          </tr>
          <tr>
            <td>State:</td>
            <td>Regular</td>
          </tr>
          <tr>
            <td>Quantity:</td>
            <td> <input type="text" id="Quantity2" name="Quantity2" value="0" /></td>
          </tr>
        </table>

        <tr>
          <td> &nbsp;</td>
          <td>&nbsp;</td>
        </tr>

        <table>
          <tr>
            <td>Widget Model:</td>
            <td>93ZZ-A</td>
          </tr>
          <tr>
            <td>Price per Unit:</td>
            <td>$28.99 <input type="hidden" id="Price3" name="Price3" value="28.99" /></td>
          </tr>
          <tr>
            <td>State:</td>
            <td>Regular</td>
          </tr>
          <tr>
            <td>Quantity:</td>
            <td> <input type="text" id="Quantity3" name="Quantity3" value="0" /></td>
          </tr>
        </table>

        <tr>
          <td> &nbsp;</td>
          <td>&nbsp;</td>
        </tr>

        <table>
          <tr>
            <td>Product Total:</td>
            <td>
              <p id="ProductTotal"> 0 </p>
            </td>
          </tr>

          <tr>
            <td> <input type="checkbox" id="extra" name="extra"> </td>
            <td>
              <p id="shipping">If the quantity exceeds 30 units, there will be extra shipping!</p>
            </td>
          </tr>
        </table>

      </div>

      <tr>
        <td> <input type="Submit" value="Submit" /> </td>
        <td> <input type="button" value="Calculate" onClick="total()" /> </td>
      </tr>

    </form>
</body>

</html>

最佳答案

您的值 qty1、qty2、qty3 有问题。这些值作为字符串读取。因此,它不是添加这些值,而是连接字符串。替换

  var qty1 = document.getElementById("Quantity1").value;
  var qty2 = document.getElementById("Quantity2").value;
  var qty3 = document.getElementById("Quantity3").value;

  var qty1 = parseInt(document.getElementById("Quantity1").value);
  var qty2 = parseInt(document.getElementById("Quantity2").value);
  var qty3 = parseInt(document.getElementById("Quantity3").value);

它将解决您的“红色”问题。

对于提交按钮,函数total() 不返回任何内容。所以改变一些东西,比如

  function total() {

   var p1 = document.getElementById("Price1").value; //Value is referenced to the input tag
   var p2 = document.getElementById("Price2").value;
   var p3 = document.getElementById("Price3").value;
   var qty1 = parseInt(document.getElementById("Quantity1").value);
   var qty2 = parseInt(document.getElementById("Quantity2").value);
   var qty3 = parseInt(document.getElementById("Quantity3").value);
   var over = qty1 + qty2 + qty3;

   if (realNumber()) {
    var totals = (p1 * qty1) + (p2 * qty2) + (p3 * qty3);
    var yes = (totals).toFixed(2);
    document.getElementById("ProductTotal").innerHTML = "$" + yes;
    if (over > 30) {
      document.getElementById("shipping").style.color = "red";
      return true;

    } else if(over>0) {
     document.getElementById("shipping").style.color = "black";
     return true;
    }else{
     document.getElementById("shipping").style.color = "black";
     return false;
    }
 }
 }

checkout()

function checkOut() {
  if (total()) {
    if (((document.getElementById("shipping").style.color == "red") &&
         (document.getElementById("extra").checked))||
        (document.getElementById("shipping").style.color != "red")) {

         return true;
    }
  }
  return false;

}

关于javascript - HTML 代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47027811/

相关文章:

javascript - 单击按钮时应用 Css

javascript - 使用 Jasmine 模拟 firebase 调用

css - 为什么段落右侧有额外空间?

HTML 箭头 "Top of Site"不在 URL 中显示#id

javascript - 呈现最初隐藏的 HTML 元素的正确方法

javascript - jquery Ajax 加载内容动画?

javascript - Jquery 在动态 html 上查找操作

javascript - jquery 自动完成验证输入的值

html &nbsp 在间距上有符号

javascript - 将按钮添加到循环生成的表中的表行