admin管理员组文章数量:1336340
I have two form fields money
and years
that need to output a number into a third field cv
.
Ideally while the first numbers are being entered but I'm OK with it happening after a button is pressed as well
<FORM name="salary">
<input type="number" size=12 name="money">
<input type="number" size=12 name="years">
<input type="number" size=12 name="cv">
</FORM>
The math involved would be the following.
(money * years) - (money * years) x = cv
Where:
- x equals 0 if years equals 1
- x equals .2 if years equals 2
- x equals .45 if years equals 3
- x equals .6 if years equals 4
- x equals .65 if years equals 5
- x equals .75 if years equals 6
I have two form fields money
and years
that need to output a number into a third field cv
.
Ideally while the first numbers are being entered but I'm OK with it happening after a button is pressed as well
<FORM name="salary">
<input type="number" size=12 name="money">
<input type="number" size=12 name="years">
<input type="number" size=12 name="cv">
</FORM>
The math involved would be the following.
(money * years) - (money * years) x = cv
Where:
- x equals 0 if years equals 1
- x equals .2 if years equals 2
- x equals .45 if years equals 3
- x equals .6 if years equals 4
- x equals .65 if years equals 5
- x equals .75 if years equals 6
- Is this homework? How far have you gotten, and where is the problem precisely? – Joel Commented Mar 9, 2009 at 23:39
- What about other values of years? – Gumbo Commented Mar 9, 2009 at 23:55
4 Answers
Reset to default 3Breaking the problem down, you need to know how to use javascript to:
- Programmatically get the text entered into a textbox
- Confirm that the textbox contains numerical data
- Perform the math
- Set the value of the third textbox
All that and much more can be found here.
You can use jQuery, in which case it'll be something like this:
var money = parseFloat($("#money").val());
var years = parseInt($("#years").val(), 10);
var x = [0.00, 0.20, 0.45, 0.60, 0.65, 0.75][years-1];
$("#cv").val((money*years)*(1-x));
Your going to want to put an onKeyUp() event on both of the fields people enter the data into and call a function something like what follows, in addition add an id to each field as used in the function. If you are not using prototype or jquery you need to use getElementById('var') in place of the $('var')
function calculateAndSetCV()
{
money = parseFloat($('money').val());
years = parseInt($('years').val());
cv = 0;
if(years == 1)
{
cv = (money * years) - (money * years)0;
}
$('cv').val(cv);
}
Do the math:
(money * years) - (money * years) x = cv
(money * years) x = (money * years) - cv
x = ((money * years) - cv) / (money * years)
x = 1 - cv / (money * years)
Get jQuery at http://jquery./
Update your code like this (notice that name → id):
<script type="text/javascript" src="jquery.js"></script>
<input type="number" size="12" id="money" />
<input type="number" size="12" id="years" />
<input type="number" size="12" id="cv" />
<div id="result"></div>
<script type="text/javascript">
$ ('input').bind ('input change keyup keydown keypress', function () {
$ ('#result').val (
1 - $ ('#cv').val () / ($ ('#money').val () * $ ('#years').val ())
)
})
</script>
本文标签: javascript math equationStack Overflow
版权声明:本文标题:javascript math equation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374633a2462915.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论