简体   繁体   English

基于Ajax请求的jQuery计算

[英]Jquery calculation on Ajax request

I have several inputs and what I want to do is calculate them on real time, so for example if you change the per_day input depending on that input all other should calculate dynamically , also if I change the per_year input all other should take the value depending on the value of that input, and It works fine till I get the ajax request, on success I set the value of 4 inputs and based on that four other inputs should take the value depending on these four but it sets only these four inputs and calculation doesn't work. 我有几个输入,而我想做的就是实时计算它们,例如,如果您根据输入更改per_day输入,所有其他输入都应动态计算,如果我更改per_year输入,所有其他输入也应取值取决于根据该输入的值,直到我收到ajax请求为止,它都可以正常工作;成功后,我设置4个输入的值,并基于其他四个输入应取值取决于这四个输入,但它仅设置了这四个输入,并且计算不起作用。

<input id="per_hour" placeholder="per_hour" class="empCosts" type="number">
<input id="hour" placeholder="hour" class="empCosts" type="number">
<input id="per_day" placeholder="per_day" class="empCosts" type="number">
<input id="day" placeholder="day" class="empCosts" type="number">
<input id="per_month" placeholder="per_month" class="empCosts" type="number">
<input id="months" placeholder="months" class="empCosts" type="number">
<input id="per_year" placeholder="per_year" class="empCosts" type="number">

and I calculate them like this 我这样计算它们

function updateCosts(id) {
    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;
    var perHourVal = perDay / hours;

    if(id === 'per_day') {

        $("#per_hour").val(Math.ceil(perHourVal));
    }
    else if(id === 'per_month'){
        perdayVal = Math.ceil(perMonth / days);
        $("#per_hour").val(Math.ceil(perHourVal));
    }
    else if(id === 'per_year'){
        perMonthVal = Math.ceil(perYear / month);
        perdayVal = Math.ceil(perMonth / days);
        $("#per_hour").val(Math.ceil(perHourVal));
    }

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

}

and this to wok on real time i call it like this 这可以实时唤醒,我这样称呼它

$('.empCosts').on('input', function () {
        var id = $(this).attr('id');
        updateCosts(id);
    });

this is my ajax 这是我的阿贾克斯

$.ajax({
            url:"/red/satu",
            type: "GET",
            data: {user: user},
            success: function (data) {
                $('#per_hour').val(fer.pay.per_hour);
                $("#hour").val(fer.pay.hours);
                $('#day').val(fer.pay.days);
                $("#months").val(fer.pay.months);
                updateCosts("per_day");

            }

        });

and on ajax success if I call the function updateCosts("per_day") 3 times it works , with one time it doesn't, why is this so? 在ajax成功的情况下,如果我将函数updateCosts(“ per_day”)调用了3次,但一次却没有,为什么会这样呢? Ajax is loaded first just for info 首先加载Ajax只是为了提供信息

The main problem is that your calculation logic isn't well thought out. 主要的问题是您的计算逻辑没有被深思熟虑。 The first time updateCosts() is called, it calculates the per day value correctly, but the per month and per year calculations are based on blank values and therefore evaluate to NaN . 第一次调用updateCosts() ,它会正确计算每天的值,但是每月和每年的计算是基于空白值的,因此计算结果为NaN That's why you have to call it 3 times in order for the values to stabilize. 这就是为什么您必须调用3次才能使值稳定。 Then you have a few haphazard calculations after that which you carry out based on which field was modified, and they probably recalculate some of the fields correctly, but not all of them. 然后,您将根据修改的字段进行一些偶然的计算,然后它们可能会正确地重新计算某些字段,但不是全部。

A better strategy is to pick one per-N value as the baseline, determine the new value for that based on whichever field was modified, and then use that as the starting point for the rest of the calculations. 更好的策略是选择每个N值作为基线,根据修改的字段确定该值的新值,然后将其用作其余计算的起点。

The following appears to work: 以下内容似乎起作用:

 function updateCosts(id) { 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 perHourVal; if (id === 'per_day') { perHourVal = perDay / hours; } else if (id === 'per_month') { perHourVal = perMonth / days / hours; } else if (id === 'per_year') { perHourVal = perYear / month / days / hours; } else { perHourVal = perHour; } var perDayVal = perHourVal * hours; var perMonthVal = perDayVal * days; var perYearVal = perMonthVal * month; $("#per_hour").val(perHourVal); $("#per_day").val(perDayVal); $("#per_month").val(perMonthVal); $("#per_year").val(perYearVal); } $('.empCosts').on('input', function(e) { e.stopPropagation(); var id = $(this).attr('id'); updateCosts(id); }); $('#per_hour').val(12); $("#hour").val(5); $('#day').val(7); $("#months").val(9); updateCosts('per_hour'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class='empCosts'> <tr> <td>Per Hour</td> <td> <input id="per_hour" placeholder="per_hour" class="empCosts" type="number"> </td> <td>Hours</td> <td> <input id="hour" placeholder="hour" class="empCosts" type="number"></td> </tr> <tr> <td>Per Day></td> <td> <input id="per_day" placeholder="per_day" class="empCosts" type="number"></td> <td>Days</td> <td> <input id="day" placeholder="day" class="empCosts" type="number"></td> </tr> <tr> <td>Per Month</td> <td> <input id="per_month" placeholder="per_month" class="empCosts" type="number"></td> <td>Months</td> <td> <input id="months" placeholder="months" class="empCosts" type="number"></td> </tr> <tr> <td>Per Year</td> <td> <input id="per_year" placeholder="per_year" class="empCosts" type="number"></td> <td></td> <td></td> </tr> </div> 

As a side note, please don't do this as a half-a**ed way of writing an if statement: 附带说明一下,请勿将其作为编写if语句的半途而废:

id !== "per_day" && $("#per_day").val(perdayVal);

JavaScript has a syntactical construct for conditionally executing code. JavaScript具有用于条件执行代码的语法构造。 It's called an if -statement. 这称为if语句。

How about keyup events? 键盘事件如何?

 $( "#input_source" ) .keyup(function() { var source_value = $( this ).val(); //Perform calculations here!!! $( "#input_target" ).val( source_value ); }) .keyup(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> input_source: <br> <input id="input_source" type="text" placeholder="type here" value=""> <br> input_target: <br> <input id="input_target" type="text" placeholder="" value=""> </form> 

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

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