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 = '£'+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/>
£<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); ?>
相关文章:
- Calculation in javascript
- Jquery Form Calculation not aligning
- 为什么我在尝试使用 StageWebView 在 AIR Android 应用程序中实现 Google Maps Jav
- Javascript Calculation - NaN message
- JavaScript Form Calculation with Radio Buttons & Textbox
- 是 var { route, redirect, routeHandler, Link } = router;在 Jav
- JQuery - Table Calculation
- Adobe Livecycle Designer Javascript Calculation
- 如何仅针对某些情况(如特定于Internet Explorer的CSS或特定于Internet Explorer的Jav
- Understanding Vertical Movement Method in "Eloquent Jav
- Tacos AjaxEventSubmit没有't在刷新后为Tapestry Palette组件附加必要的jav
- Bootstrap (Twitter) Datepicker Difference Calculation Return
- 如何使用moments .js解析“今天”等英语单词“tomorrow"“去年friday"到Jav
- TypeError: Cannot read property 'style' of null (Jav
- Javascript det calculation
- calculation with && and OR
- 使用在Visual Studio 2010中从mindscape web工作台(coffeescript源)生成的jav
- php and javascript calculation
- Django 1.10 -使用Django .shortcut .render生成一个带有变量的网页,其中包含一个jav
- Calculation with JQuery/javascript