使用此

时间:2018-01-19 10:53:15

标签: javascript jquery

我正在尝试在输入之间进行一些计算,我有7个输入,我想要做的是在它们之间进行计算,所以这是我的代码

      $('.empCosts').on('input', function () {
        var perHour = parseInt($('#per_hour').val());
        var hours = parseInt($('#hour').val());
        var perDay = parseInt($('#per_day').val());
        var days = parseInt($('#day').val());
        var perMonth = parseInt($('#per_month').val());
        var month = parseInt($('#months').val());
        var perYear = parseInt($('#per_year').val());
        var perdayVal = parseInt(perHour * hours);
        var perMonthVal = parseInt(perDay * days);
        var perYearVal = parseInt(perMonth * month);
        var perHourVal = parseInt(perDay / hours);
        var perMonthDivide = parseInt(perYearVal / month);

        $("#per_day").val(perdayVal);
        $("#per_month").val(perMonthVal);
        $("#per_year").val(perYearVal);
        $("#per_hour").val(perHourVal);
        if($(this).is(":focus")){
            alert('sdfasdfad');
        }
    });

这些输入应该连接你的乘法或除法,当我尝试更改每日值时它不会改变,因为我已经用var perdayVal = parseInt(perHour * hours);设置了该输入的值,我希望你增加数字或减少它,它应该使计算乘以或除以输入,这里你有小提琴

DEMO

1 个答案:

答案 0 :(得分:0)

试试这个https://jsfiddle.net/2jb2e3Lv/7/

$('.empCosts').on('input', function () {
    var perHour = parseInt($('#per_hour').val());
    var hours = parseInt($('#hour').val());
    var perDay = parseInt($('#per_day').val());
    var days = parseInt($('#day').val());
    var perMonth = parseInt($('#per_month').val());
    var month = parseInt($('#months').val());
    var perYear = parseInt($('#per_year').val());
    var perdayVal = perHour * hours;
    var perMonthVal = perDay * days;
    var perYearVal = perMonth * month;


    $(this).attr("id") !== "per_day" && $("#per_day").val(perdayVal);
    $(this).attr("id") !== "per_month" && $("#per_month").val(perMonthVal);
    $(this).attr("id") !== "per_year" && $("#per_year").val(perYearVal);
});