简体   繁体   English

从多个输入字段中减去值

[英]Subtracting values from multiple input fields

I'm using the following code to take the value of one field, subtract it from the value of another field and display the result. 我正在使用以下代码来获取一个字段的值,从另一字段的值中减去它并显示结果。

$(document).ready(function() {  

    var numIn;
    var numOut;
    var total;

    $('#submit').click(function() {
        numIn = $('input.in').val();
        numOut = $('input.out').val();
        total = numIn-numOut;

        $('span').remove();
        $('body').append('<span>&#163;'+total+'</span>');
        $('span').fadeIn(250);
    });
});

I want to create a sort of income/expenditure calculator. 我想创建一种收入/支出计算器。 so my question is, say I had multiple income fields and multiple expenditure fields how would I take the total value from the income fields away from the total value of the expenditure fields. 所以我的问题是,比如说我有多个收入领域和多个支出领域,我该如何从收入领域的总价值中减去支出领域的总价值。

Here is an example form in case I haven't been clear. 如果我不清楚,这是示例表格。

<form>
<input class="in" type="text">
<input class="in" type="text">
<input class="in" type="text">
<input class="in" type="text">
<input class="out" type="text">
<input class="out" type="text">
<input class="out" type="text">
<input class="out" type="text">
<input type="submit" id="submit" value="Submit">

You could loop over the .in and .out fields, and add their values as you go. 您可以遍历.in.out字段,并随需添加它们的值。

Example: http://jsfiddle.net/tXPeg/3/ 示例: http //jsfiddle.net/tXPeg/3/

var numIn = 0;
var numOut = 0;
var total;

$('#submit').click(function() {
    $('input.in').each(function() {
        return numIn += (parseFloat(this.value) || 0);
    });
    $('input.out').map(function() {
        return numOut += (parseFloat(this.value) || 0);
    });
    total = numIn - numOut;

    $('span').remove();
    $('body').append('<span>&#163;' + total + '</span>');
    $('span').fadeIn(250);
    return false;
});​

EDIT: Changed to use parseFloat() instead of parseInt() . 编辑:更改为使用parseFloat()代替parseInt()

Actually after testing a little further the only problem is that if one or more fields are empy it returns 'NaN'. 实际上,在进一步测试之后,唯一的问题是,如果一个或多个字段为空,则返回“ NaN”。

I tried adding conditional statements to check that the field had a value but no luck: Feel free to edit my example: http://jsfiddle.net/QaEcD/3/ 我尝试添加条件语句来检查该字段是否具有值,但没有运气:请随时编辑我的示例: http : //jsfiddle.net/QaEcD/3/

$('#submit').click(function() {
    var numIn = 0;
    var numOut = 0;
    var total = 0;

    $('input.in').each(function() {
        if($(this).val().length !== 0){
            return numIn = numIn + parseInt(this.value);
        });
    });
    $('input.out').map(function() {
        if($(this).val().length !== 0){
            return numOut = numOut + parseInt(this.value);
        });
    });

    total = numIn-numOut;

    $('span').remove();
    $('body').append('<span>&#163;'+total+'</span>');
    $('span').fadeIn(250);
    return false;
});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM