A small example of JS averaging


Effect:

Event: the blur event is triggered when the three text fields are entered. Calculate the average

Description: call the method to calculate the average value,

Code:

In the text box:

<asp:TextBox ID="tblmyd11" Field="lmyd11" TableName="gqls_sy" EnabledAction="(12.*)"  CssClass="erp_textbox_short" runat="server" self="ydpj"></asp:TextBox>

JS:

        $(function() {
            $("[self='ydpj']").blur(function() { Getlmpj("11", "21", "31", "41"); });
            $("[self='ydpj']").blur(function() { Getlmpj("12", "22", "32", "42"); });
            $("[self='ydpj']").blur(function() { Getlmpj("13", "23", "33", "43"); });
            $("[self='ydpj']").blur(function() { Getlmpj("14", "24", "34", "44"); });
            // nut
            function Getlmpj(one, two, three, four) {
                var avg = 0;
                var num1 = ChangeFloat($("input[id*=tblmyd" + one + "]").val());
                var num2 = ChangeFloat($("input[id*=tblmyd" + two + "]").val());
                var num3 = ChangeFloat($("input[id*=tblmyd" + three + "]").val());
                if (num1 != 0 && num2 != 0 && num3 != 0) {
                    avg = (num1 + num2 + num3) / 3;
                    $("input[id*=tblmyd" + four + "]").val(RoundValue(avg, 0.1, 1));
                }
                else {
                    $("input[id*=tblmyd" + four + "]").val("");
                }
            }
        });
function ChangeFloat(data) {
            if (data != null && data != "") {
                return parseFloat(data);
            }
            else {
                return 0;
            }
        }