JavaScript 数学方程

发布于 2024-07-14 14:07:10 字数 636 浏览 4 评论 0原文

我有两个表单字段 moneyyears ,需要将数字输出到第三个字段 cv

理想情况下,当输入第一个数字时,但我也同意按下按钮后发生这种情况,

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

涉及的数学如下。

(金钱 * 年数)-(金钱 * 年数)x = 简历

其中:

  • x 等于 0 如果年数等于 1
  • x 等于 .2 如果年数等于 2
  • x 等于 .45 如果年数等于 3
  • x 等于 .6 如果年数等于 4
  • x 等于 .2。如果年数等于 5,则为 65
  • x 如果年数等于 6,则等于 0.75

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱她像谁 2024-07-21 14:07:10

分解问题,您需要知道如何使用 javascript 来:

  • 以编程方式将文本输入到文本框中
  • 确认文本框包含数字数据
  • 执行数学运算
  • 设置第三个文本框的值

所有这些以及更多内容都可以找到 此处

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.

把时间冻结 2024-07-21 14:07:10

您可以使用 jQuery,在这种情况下,它将是这样的:

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));

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));
放飞的风筝 2024-07-21 14:07:10

您需要在人们输入数据的两个字段上放置一个 onKeyUp() 事件,并调用如下所示的函数,此外还向函数中使用的每个字段添加一个 id。 如果您不使用prototype或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);
}

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);
}
挽梦忆笙歌 2024-07-21 14:07:10

计算一下:

(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>

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.com/

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文