<?php
$script = "
window.addEvent('domready', function() {
function calculate() {
var value1 = $('value1').value;
var value2 = $('value2').value;
var value3 = $('value3').value;
var value4 = $('value4').value;
var value5 = $('value5').value;
var total = ( value1 + value2 + value3 + value4 + value5);
// Check that the result is a finite number. If so, display the results.
if (!isNaN(monthly) &&
(monthly != Number.POSITIVE_INFINITY) &&
(monthly != Number.NEGATIVE_INFINITY)) {
total.value = round(value1 +value2 +value3 +value4 + value5);
} else {
// Otherwise, the user's input was probably invalid, so don't
// display anything.
total.value = '';
}
}
// rounds number to two decimal places.
function round(x) {
return Math.round(x*100)/100;
}
$('compute').addEvent('click', calculate );
$('value1').addEvent('change', calculate );
$('value2').addEvent('change', calculate );
$('value3').addEvent('change', calculate );
$('value4').addEvent('change', calculate );
$('value5').addEvent('change', calculate );
});
";
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration($script);
?>
<table>
<tr><td colspan="3"><b>Enter Amount Information:</b></td></tr>
<tr>
<td>1)</td>
<td>Amount of the 1 :</td>
<td><input type="text" name="value1" id="value1" size="12" ></td>
</tr>
<tr>
<td>2)</td>
<td>Amount of the 2 :</td>
<td><input type="text" name="value2" id="value2" size="12" ></td>
</tr>
<tr>
<td>3)</td>
<td>Amount of the 3 :</td>
<td><input type="text" name="value3" id="value3" size="12" ></td>
</tr>
<tr>
<td>4)</td>
<td>Amount of the 4 :</td>
<td><input type="text" name="value4" id="value4" size="12" ></td>
</tr>
<tr>
<td>5)</td>
<td>Amount of the 5 :</td>
<td><input type="text" name="value5" id="value5" size="12" ></td>
</tr>
<tr><td colspan="3">
<input type="button" value="Compute" id='compute' " >
</td></tr>
<tr><td colspan="3">
<b>Payment Information:</b>
</td></tr>
<tr>
<td>5)</td>
<td>Your total will be:</td>
<td><input type="text" name="total" id="total" size="12" readonly='readonly'></td>
</tr>
</table>
?>
此致 ADik垫
答案 0 :(得分:1)
应该是
$('#value1').val()
而不是
$('value1').value
如果您正在使用jquery并按ID
选择元素答案 1 :(得分:0)
删除?&gt;在代码的最后
更改为此代码
<input type="button" value="Compute" id='compute' />
使用
$(document).ready(function() { } );
如果您已经在使用jQuery。
<body></body>
标签
这意味着你应该从
开始<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
//put some contents like table
</body>
</html>
$(':text').change(function() {
var sum = 0;
$(this).each(function() {
sum += $(this).val();
});
$("#total").val(sum);
});