用户在HTML Javascript中输入的所有数字的总和

时间:2017-09-26 19:48:17

标签: javascript html

我需要帮助来创建一个函数:

  1. 包含用户目前输入的所有数字总和的信息,计算,打印到控制台,并存储在HTML页面上。
  2. 当总和大于100时,该功能会打印"它超过"控制台。
  3. 有什么建议吗?以下是我到目前为止所做的事情:

    
    
    ?php require_once('Connections/Systems.php');
    require_once('calendar/classes/tc_calendar.php');?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", 
    $theNotDefinedValue = "") 
    {
    if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }
    
    $theValue = function_exists("mysql_real_escape_string") ? 
    mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
    switch ($theType) {
      case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
      case "long":
      case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
      case "double":
       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
      case "date":
       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
      case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
    }
    return $theValue;
      }
      }
    
      $editFormAction = $_SERVER['PHP_SELF'];
      if (isset($_SERVER['QUERY_STRING'])) {
     $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
     }
    
      if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO inctickets (RecordID, INCNum, Requester, 
     Category, Details, Status, DateSubmitted, CompletedBy, Comments, 
      DateChanged) 
        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['RecordID'], "int"),
                       GetSQLValueString($_POST['INCNum'], "text"),
                       GetSQLValueString($_POST['Requester'], "text"),
                       GetSQLValueString($_POST['Category'], "text"),
                       GetSQLValueString($_POST['Details'], "text"),
                       GetSQLValueString($_POST['Status'], "text"),
                       GetSQLValueString($_POST['DateSubmitted'], "date"),
                       GetSQLValueString($_POST['CompletedBy'], "date"),
                       GetSQLValueString($_POST['Comments'], "text"),
                       GetSQLValueString($_POST['DateChanged'], "date"));
    
     mysql_select_db($database_Systems, $Systems);
     $Result1 = mysql_query($insertSQL, $Systems) or die(mysql_error());
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>INC Ticket</title>
     <script language="javascript" src="calendar.js"></script>
     </head>
    
      <body>
      <p><a href="./SystemsDashboard.php"><img border="0" alt="menu" 
     src="penskelogo.jpg"></a>
     </p>
      <h2 align="center">Insert Request</h2>
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" 
      id="form1">
      <table align="center">
      <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>Help Desk Inc#:</strong></td>
       <td><input type="text" name="INCNum" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>Requester:</strong></td>
      <td><input type="text" name="Requester" value="" size="32" /></td>
       </tr>
       <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>Category:</strong></td>
      <td><select name="Category">
      <option value="Computer Issue" <?php if (!(strcmp("Computer Issue", ""))) 
       {echo "SELECTED";} ?>>Computer Issue</option>
        <option value="Email Issue" <?php if (!(strcmp("Email Issue", ""))) 
       {echo "SELECTED";} ?>>Email Issue</option>
        <option value="Internet Issue" <?php if (!(strcmp("Internet Issue", 
       ""))) {echo "SELECTED";} ?>>Internet Issue</option>
          <option value="Label Printer Issue" <?php if (!(strcmp("Label Printer 
       Issue", ""))) {echo "SELECTED";} ?>>Label Printer Issue</option>        
        <option value="Network Connection Issue" <?php if (!(strcmp("Network 
      Connection Issue", ""))) {echo "SELECTED";} ?>>Network Connection 
      Issue</option>
        <option value="Other" <?php if (!(strcmp("Other", ""))) {echo 
       "SELECTED";} ?>>Other</option>
        <option value="Printer Issue" <?php if (!(strcmp("Printer Issue", ""))) 
       {echo "SELECTED";} ?>>Printer Issue</option>
        <option value="SAP Connection Issue" <?php if (!(strcmp("Internet 
       Issue", ""))) {echo "SELECTED";} ?>>SAP Connection Issue</option>
          <option value="SAP Program Issue" <?php if (!(strcmp("Internet Issue", 
      ""))) {echo "SELECTED";} ?>>SAP Program Issue</option>
      </select></td>
      </tr>
      <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top"><strong>Details:</strong>
      </td>
      <td><textarea name="Details" cols="50" rows="5"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>Status:</strong></td>
      <td><select name="Status">
        <option value="Pending" <?php if (!(strcmp("Pending", ""))) {echo 
    "SELECTED";} ?>>Pending</option>
    
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>Date Submitted:</strong></td>
      <td><span class="Arial">
        <?php
     $myCalendar = new tc_calendar("DateSubmitted", true, false);
    $myCalendar->setIcon("images/iconCalendar.gif");
    $myCalendar->setDate(date('d'), date('m'), date('Y'));
    $myCalendar->setPath("calendar/");
    $myCalendar->setYearInterval(2000, 2025);
    $myCalendar->dateAllow('2008-05-13', '2025-03-01');
    $myCalendar->setDateFormat('j F Y');
    $myCalendar->setAlignment('left', 'bottom');
    //output the calendar
      $myCalendar->writeScript();
     ?>
      </span></td>
      </tr>
      <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record" /></td>
      </tr>
      </table>
    
      <p align="center">&nbsp;</p>
      <p>
      <input type="hidden" name="RecordID" value="" />
      <input type="hidden" name="CompletedBy" value="" />
      <input type="hidden" name="Comments" value="" />
      <input type="hidden" name="MM_insert" value="form1" />
      </p>
      </form>
      <p>&nbsp;</p>
      </body>
      </html>enter code here
    
    &#13;
    function adder() {
      var num1 = parseInt(document.getElementById("myform").elements["num1"].value);
      var num2 = parseInt(document.getElementById("myform").elements["num2"].value);
      var total = num1 + num2;
      document.getElementById("p1").innerHTML = total;
    }
    &#13;
    &#13;
    &#13;

2 个答案:

答案 0 :(得分:2)

您应该在total之外定义0adder(),以便您可以将字段值添加到现有总和中。这是一个例子:

var total = 0;

function adder() {
  var num1 = parseInt(document.getElementById("myform").elements["num1"].value);
  var num2 = parseInt(document.getElementById("myform").elements["num2"].value);
  total += num1 + num2;
  document.getElementById("p1").innerHTML = total;
  if (total > 100) console.log("It's over!");
}
<form id="myform">
  First Number: <input type="text" name="num1"><br /><br /> Second Number: <input type="text" name="num2"><br /><br />
</form>
<button onclick="adder()"> Submit</button><br><br>
<p id="p1">Results Are Here</p>

如果您的<button>属于<form>,则会导致页面刷新,因此在这种情况下添加type="button"可以解决此问题,您可以看到结果得当。

答案 1 :(得分:1)

快速修改到answer以上,以便在到达&gt;之后停止计数100。

&#13;
&#13;
var total = 0;

function adder() {
  var num1 = parseInt(document.getElementById("myform").elements["num1"].value);
  var num2 = parseInt(document.getElementById("myform").elements["num2"].value);
  if (total > 100){
    console.log("It's over!");
  } else {
    total += num1 + num2;
    document.getElementById("p1").innerHTML = total;
  }
}
&#13;
<form id="myform">
  First Number: <input type="text" name="num1"><br /><br /> Second Number: <input type="text" name="num2"><br /><br />
</form>
<button onclick="adder()"> Submit</button><br><br>
<p id="p1">Results Are Here</p>
&#13;
&#13;
&#13;