Javascript计算
下面的编码输出值显示在div标签中。但我想显示在文本框中。还有一个我想要的第一个文本框值是来自使用php的db。请帮帮我Javascript计算,javascript,Javascript,下面的编码输出值显示在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 {
<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>
.测试{
颜色:红色;
}
#总数{
字体大小:40px;
}
h1{
裕度:0 10px 0;
文字装饰:下划线;
文本对齐:居中;
}
氢{
裕度:0 10px 0;
}
计算=函数(totalElement)
{
if(totalElement)
{
var计算=“”;
var总体=“”;
变量字段=新数组();
var theElement=document.getElementById(totalElement);
var userInputs=myform.elements;
var_type='';
对于(var f=0;f 在文本框中显示(var i=0;i很容易,而不是:
theDiv.innerHTML = 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>
.测试{
颜色:红色;
}
#总数{
字体大小:40px;
}
h1{
裕度:0 10px 0;
文字装饰:下划线;
文本对齐:居中;
}
氢{
裕度:0 10px 0;
}
计算=函数(totalElement)
{
if(totalElement)
{
var计算=“”;
var总体=“”;
变量字段=新数组();
var theElement=document.getElementById(totalElement);
var userInputs=myform.elements;
var_type='';
对于(var f=0;f 对于(var i=0;i在div中插入输入标记:
<div id="total"><input type="text" id="theresultdiv" value="0" /></div>
正如所建议的,您可以使用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); ?>
嗯,您的代码不完整。复制粘贴问题?是的。但我还是需要解决方案。如果您有办法帮助我。如果您的解决方案部分是eval,则您不知道问题出在哪里。
<?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);
?>
<?php echo($iResult); ?>