Javascript Calculation

Javascript Calculation

本文关键字:Calculation Javascript      更新时间:2023-09-26

这里,下面的输出值编码显示在div标记中。但我想显示在文本框中。另一个我想要的第一个文本框值来自使用php的db。请帮帮我。

<html>
<head>
<style>
.test {
    color:red;
}
#total {
    font-size:40px;
}
h1 {
    margin: 0 0 10px 0;
    text-decoration: underline;
    text-align: center;
}
h2 {
    margin: 0 0 10px 0;
}
</style>
<script>
  calculate = function(totalElement)
  {
      if (totalElement)
      {   
          var calculation  = '';
          var overall = '';
          var fields = new Array();
          var theElement = document.getElementById(totalElement);
          var userInputs = myform.elements;
          var the_type = '';
          for (var f = 0; f < userInputs.length; f++)
          { 
              if (userInputs[f].className=='special_value')
              {
                  if (userInputs[f].type=='select-one')
                  {
                      if(userInputs[f].options[userInputs[f].selectedIndex].value)
                      {
                          fields[f] = userInputs[f].options[userInputs[f].selectedIndex].value;
                      }
                      else
                      {
                          fields[f] = 0;
                      }
                  }
                  else if(userInputs[f].type=='radio' || userInputs[f].type=='checkbox')
                  {
                      if (userInputs[f].checked)
                      {
                          fields[f] = userInputs[f].value;
                      }
                      else
                      {
                          fields[f] = 0;
                      }
                  }
                  else
                  {
                      if (userInputs[f].value)
                      {
                          fields[f] = userInputs[f].value;
                      }
                      else
                      {
                          fields[f] = 0;
                      }
                  }
              }
          }
          for (var i=0; i<fields.length; i++)
          { 
              calculation += fields[i];
              if (i!=fields.length-1)
              {
                  calculation += '+';
              }
          }
          if (calculation!='')
          {
              overall = eval(calculation).toFixed(2);
          }
          if (overall!='')
          {
              theElement.innerHTML = '&pound;'+overall;
          }
      }
  }
</script>
</head>
<body>
<form name="myform">
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price"> <Br/>
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price2"> <Br/>
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price4"> <Br/>
<div id="total"></div>
<input onClick="calculate('total');" type="button" name="button" value="re-calculate">
</form>
</body>
</html>

在文本框中显示很容易,而不是:

theDiv.innerHTML = value;

进行

theTextbox.value = value;

要用php数据填充它,应该使用Ajax,请研究一下。希望这能有所帮助。干杯

试试这个,它会很好。

<?php
    $dbPrice = 20;// replace this by your query result
?>
<html>
<head>
<style>
.test {
    color:red;
}
#total {
    font-size:40px;
}
h1 {
    margin: 0 0 10px 0;
    text-decoration: underline;
    text-align: center;
}
h2 {
    margin: 0 0 10px 0;
}
</style>
<script>
  calculate = function(totalElement)
  {
      if (totalElement)
      {
          var calculation  = '';
          var overall = '';
          var fields = new Array();
          var theElement = document.getElementById(totalElement);
          var userInputs = myform.elements;
          var the_type = '';
          for (var f = 0; f < userInputs.length; f++)
          {
              if (userInputs[f].className=='special_value')
              {
                  if (userInputs[f].type=='select-one')
                  {
                      if(userInputs[f].options[userInputs[f].selectedIndex].value)
                      {
                          fields[f] = userInputs[f].options[userInputs[f].selectedIndex].value;
                      }
                      else
                      {
                          fields[f] = 0;
                      }
                  }
                  else if(userInputs[f].type=='radio' || userInputs[f].type=='checkbox')
                  {
                      if (userInputs[f].checked)
                      {
                          fields[f] = userInputs[f].value;
                      }
                      else
                      {
                          fields[f] = 0;
                      }
                  }
                  else
                  {
                      if (userInputs[f].value)
                      {
                          fields[f] = userInputs[f].value;
                      }
                      else
                      {
                          fields[f] = 0;
                      }
                  }
              }
          }
          for (var i=0; i<fields.length; i++)
          {
              calculation += fields[i];
              if (i!=fields.length-1)
              {
                  calculation += '+';
              }
          }
          if (calculation!='')
          {
              overall = eval(calculation).toFixed(2);
          }
          if (overall!='')
          {
              theElement.value = overall;
          }
      }
  }
</script>
</head>
<body>
<form name="myform">
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" value="<?php echo $dbPrice;?>" type="text" name="price"> <Br/>
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price2"> <Br/>
<input onKeyPress="calculate('total');" onBlur="calculate('total');" class="special_value" type="text" name="price4"> <Br/>
&pound;<input class="special_value" value="" id="total" type="text" name="total"> <Br/>
<!-- <div id="total"></div> -->
<input onClick="calculate('total');" type="button" name="button" value="re-calculate">
</form>
</body>
</html>

在div中插入输入标签:

<div id="total"><input type="text" id="theresultdiv" value="0" /></div>

而不是Element.innerHTML='£'+全面的你应该放置这个代码:

document.getElemenbById('theresultdiv').value = overall;

正如建议您的那样,您可以使用AJAX从DB获取数据。但在您的情况下,最好使用简单的代码:

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can''t use foo : ' . mysql_error());
}
$rResult = mysql_query("SELECT start_value FROM your_table WHERE start_value > 0 LIMIT 1", $link);
$iResult = mysql_fetch_query($rResult) ;
mysql_close($link);
?>

将上面的代码放在文件的开头。值属性位置不是0:

<?php echo($iResult); ?>