ここで以下のコードは出力値がdivタグに表示されます。しかし、私はテキストボックスに表示したい。もう1つは、最初のテキストボックスの値がPHPからphpを使用してdbになっていることです。私を助けてください。JavaScriptの計算
<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>
ええと、あなたのコードは、すべてそこではありません。コピーペーストの問題? – tjameson
「php」とタグ付けされているのはなぜですか? – wonk0
はい。とにかく私は解決策が欲しい。もしあなたが私を助けてくれるなら。 – boopathi