简体   繁体   English

javascript数组中读取表单数据的最佳实践

[英]javascript arrays reading in form data best practice

I'm still quite new to javascript and was wondering if there's a more efficient way to handle this situation, for example by using an array? 我对javascript还是很陌生,想知道是否有更有效的方法来处理这种情况,例如使用数组?

I have an HTML form with 6 fields that let you enter the last six weeks amount of overtime paid. 我有一个包含6个字段的HTML表单,可让您输入最近六周的加班费。 I'm then using javascript to add the six values up, divide by six and multiply by 52 to obtain an annual overtime amount. 然后,我使用javascript将六个值相加,除以6并乘以52,以获得年度加班费。 My fields are named w_ot_1, w_ot_2 up to w_ot_6 and I'm using the code below. 我的字段被命名为w_ot_1,w_ot_2至w_ot_6,并且我正在使用以下代码。 It all works fine but I'm finding it's really repetitive and it's not just overtime I need to run this calculation on. 一切正常,但我发现它确实是重复性的,不仅需要加班,还需要继续计算。 I'm sure there's got to be a more efficient way. 我敢肯定必须有一个更有效的方法。 Does anyone have any ideas that could help? 有没有人有什么想法可以帮助您?

var weekly_ot_1 = parseFloat(document.getElementById("w_ot_1").value.replace(/[^0-9.]/g, ''));
var weekly_ot_2 = parseFloat(document.getElementById("w_ot_2").value.replace(/[^0-9.]/g, ''));
var weekly_ot_3 = parseFloat(document.getElementById("w_ot_3").value.replace(/[^0-9.]/g, ''));
var weekly_ot_4 = parseFloat(document.getElementById("w_ot_4").value.replace(/[^0-9.]/g, ''));
var weekly_ot_5 = parseFloat(document.getElementById("w_ot_5").value.replace(/[^0-9.]/g, ''));
var weekly_ot_6 = parseFloat(document.getElementById("w_ot_6").value.replace(/[^0-9.]/g, ''));
//weekly annualised overtime values
document.getElementById("w_annual_ot").value= addCommas((((weekly_ot_1 + weekly_ot_2 + weekly_ot_3 + weekly_ot_4 + weekly_ot_5 + weekly_ot_6)/6) * 52).toFixed(2));

This is a situation where you can leverage a simple for loop and string concatenation when calling document.getElementById() . 在这种情况下,可以在调用document.getElementById()时利用简单的for循环和字符串连接。 I would suggest creating a function to calculate the overtime paid and have the function take the number of weeks as a parameter so that you can easily change it if you add more fields. 我建议创建一个函数来计算支付的加班费,并让该函数将周数作为参数,以便在添加更多字段时可以轻松更改它。

function getOvertimePaid(numberOfWeeks) {
    var total = 0;

    // Iterate from 1 to the number of weeks and increment the total by 
    // the parsed value in the field for the current index
    for (var i=1; i<=numberOfWeeks; i++) {
        total += parseFloat(document.getElementById('w_ot_' + i).value.replace(/[^0-9.]/g, '');
    }

    // Return the annualized amount as a float for flexibility
    return (total / numberOfWeeks) * 52;
 }

 // Update weekly annualised overtime values and provide formatting at this point
 document.getElementById("w_annual_ot").value= addCommas(getOvertimePaid(6).toFixed(2));

Another thing you may want to look at to make the code and the supporting HTML even more flexible is to leverage a class name on your weekly overtime input elements. 为了使代码和支持的HTML更加灵活,您可能需要查看的另一件事是在每周的加班输入元素中使用类名。 If you do that and adjust the code slightly you can add or remove fields at will and the function to calculate the annualized overtime will continue to work. 如果您这样做并稍微调整代码,则可以随意添加或删除字段,并且用于计算年度加班时间的功能将继续起作用。 As an example: 举个例子:

HTML 的HTML

<input type="text" id="w_ot_1" class="weekly-overtime" value="0.00" />
<input type="text" id="w_ot_2" class="weekly-overtime" value="0.00" />
<input type="text" id="w_ot_3" class="weekly-overtime" value="0.00" />

JavaScript 的JavaScript

function getAnnualizedValue(className) {
    // Get all elements with the given class name
    var elements = document.getElementsByClassName(className);

    // Iterate the elements and keep a running total of their values
    var total = 0;
    for (var i = 0; i < elements.length; i++) {
        total += parseFloat((elements[i].value || '0').replace(/[^0-9.]/g, '');
    }

    // Return the annualized amount as a float for flexibility
    return (total / numberOfWeeks) * 52;
}

// Update weekly annualised overtime values and provide formatting at this point
document.getElementById("w_annual_ot").value= addCommas(getAnnualizedValue('weekly-overtime').toFixed(2));

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

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