简体   繁体   中英

Jquery: How can I compute total average of each function based from the values in the input type = number?

I am new to Javascript/Jquery and I would like to compute the final rating based from the value in the text box for each function name [1st column of the table] (core function, support function, research services).

图片

The formula based from example:

  1. Final Rating Core Administrative Function = (4.6 + 4.3)/2 (because there are only 2 core functions average value is available) * 0.65
  2. Final Rating Support Function = (4.3 + 4.3)/2 * 0.2275
  3. Final Rating for Research Function = (4 + 4)/2 * 0.1225

The requirement is that everytime the A value column of each row changes the final rating change as well.

Here is the Jquery of on how I get the A value:

//GET THE AVERAGE PER ROW
$(".q-value, .e-value, .t-value").change(function(){
    let currentRow = $(this).closest('tr');
    let EValue = parseInt(currentRow.find('.e-value').val());
    let QValue = parseInt(currentRow.find('.q-value').val());
    let TValue = parseInt(currentRow.find('.t-value').val());
    currentRow.find('.a-value').val((EValue  + QValue + TValue ) / 3);
});

the.q-value, .e-value, .t-value, .a-value are all inside a class.

I add 3 input type for each final rating functions

<input type="number" class="form-control form-control-sm" id="core-total" name="total_core" readonly>
<input type="number" class="form-control form-control-sm" id="support-total" name="total_support" readonly>
<input type="number" class="form-control form-control-sm" id="research-total" name="total_research" readonly>

so the value for each function will be dump in there. Please help me on this. Im stuck in a days.

With the help of my colleague, this is already resolved. I actually hardcoded an if statement in each input type for each functions (core, support etc..)

@if($row->function_name == 'Core Functions')
                                <input type="number" onchange="setFourNumberDecimal(this)" class="form-control form-control-sm a-value-core" name="A[]" style="width: 50px" readonly>
                            @elseif($row->function_name == 'Support Functions')
                                <input type="number" onchange="setFourNumberDecimal(this)" class="form-control form-control-sm a-value-support" name="A[]" style="width: 50px" readonly>
                            @endif
                            @if($row->function_name == 'Research and Extension Services')
                                <input type="number" onchange="setFourNumberDecimal(this)" class="form-control form-control-sm a-value-research" name="A[]" style="width: 50px" readonly>
                            @endif

After that. He created a JS function for computation.

//COMPUTE AVERAGE FOR EACH FUNCTION
    function computeAvg() {
        // For Core Functions
        const corevalues = document.getElementsByClassName("a-value-core")
        let avg = 0
        let total = 0
        let count = 0
        for (let x = 0; x < corevalues.length; x++) {
            if (corevalues[x].value !== "") {
                count++
                total = total + parseFloat(corevalues[x].value)
            }
        }
        avg = (total / count) * 0.65
        $('#core-total-average').val(isNaN(avg) ? "" : avg)

        // For Support Functons
        avg = 0
        total = 0
        count = 0
        const supvalues = document.getElementsByClassName("a-value-support")
        for (let x = 0; x < supvalues.length; x++) {
            if (supvalues[x].value !== "") {
                count++
                total = total + parseFloat(supvalues[x].value)
            }
        }
        avg = total / count * 0.2275
        $('#support-total-average').val(isNaN(avg) ? "" : avg)

        // For Research Services
        avg = 0
        total = 0
        count = 0
        const resvalues = document.getElementsByClassName("a-value-research")
        for (let x = 0; x < resvalues.length; x++) {
            if (resvalues[x].value !== "") {
                count++
                total = total + parseFloat(resvalues[x].value)
            }
        }
        avg = total / count * 0.1225
        $('#research-total-average').val(isNaN(avg) ? "" : avg)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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