1

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
4

4 回答 4

3

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));
于 2009-03-09T23:44:27.870 回答
3

Breaking 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.

于 2009-03-09T23:45:24.840 回答
0

您将要在人们输入数据的两个字段上放置一个 onKeyUp() 事件并调用如下所示的函数,此外还为函数中使用的每个字段添加一个 id。如果您不使用原型或 jquery,则需要使用 getElementById('var') 代替 $('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);
}
于 2009-03-09T23:48:50.993 回答
0

算一算:

(money * years) - (money * years) x = cv
(money * years) x = (money * years) - cv
x = ((money * years) - cv) / (money * years)
x = 1 - cv / (money * years)

在http://jquery.com/获取 jQuery

像这样更新您的代码(注意名称 → 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>
于 2009-03-09T23:58:33.763 回答