保存输入值并使用旧值进行计算JQUERY

时间:2018-09-19 10:45:41

标签: javascript jquery html5

我正在尝试的是,无论用户如何更改数组的值,工作流值都必须保存在新数组中并以我的总数进行计算。

假设html是正确的。所有功能和js + jquery行都必须在一个文件中。

ex开始:值1 = 1,值= 2 在我想使用事件侦听器并推送新值1 = 5后,最终值必须为7(5 + 2)。

有什么帮助吗?

var workflow_value = [
    'Enter Value 1',
    'Enter Value 2',
    'Enter Value 3'
];

var questionsAndValues = [];

var getFieldValues = function() {

    var values = [];


    for (var i = 0; i < workflow_value.length; i++) {
        var fieldName = workflow_value[i];
        var fieldValue = college.getJobWorkflowValueByName(fieldName);
        if (fieldValue && fieldValue != '') {
            var item = {
                id: questionsAndValues.length,
                text: fieldName,
                value: fieldValue
            };
            questionsAndValues.push(item);
        }
    }
    console.log("Before update: ", workflow_value[i])
    console.log(questionsAndValues);


};


function calcValue (){
    var sumOfValues = 0;

    for (var i = 0; i < questionsAndValues.length; i++) {
        var current_value = parseFloat(questionsAndValues[i].value);
        console.log(current_value);
        if (!isNaN(current_value)) {
            sumOfValues += current_value;
            console.log(sumOfValues);
        } 
        else {
            console.log('invalid values');
        }
    }

    // questionsAndValues['Total Value' + sumOfValues] = sumOfValues;



    var rowToAppend =   '<div style="min-height:40px;border-bottom: solid 1px gray"><div style="min-width:38%; max-width:38%; display:inline-block; height:40px; border-right: solid 1px gray"><div style="padding:5%">' +  'Total Value' + '</div></div>' + 
                            '<div style="min-width:60%; max-width:60%; display:inline-block; margin-left:5px"><div>' +  sumOfValues + '</div></div></div>'

    $('#sumValues').replaceWith(rowToAppend);

      var options = {
        "title": "Total of values is " + current_value,
        "text": "The total value is " + sumOfValues,
        "ok_label":"Yes",
        "cancel_label":"Do Nothing"

    }

};


var okShowText = function(){

};

var cancelShowText = function(){
};


var onValueChanged = function() {
    console.log(arguments);

    $('#userInput-' + item.id).on('keyup', onValueChanged.bind(item));
};


$(document).ready( function() {

   getFieldValues();

    for (var i = 0; i < questionsAndValues.length; i++) {
        var item = questionsAndValues[i];
        var rowToAppend =   '<tr>' +
                                '<td >' + item.text + '</td>' + 
                                '<td><input type="number" id="userInput-' + item.id + '" size="2" value="' +  item.value + '"></input></td>' +
                            '</tr>';

        $('#userInput-' + item.id).on('keyup', onValueChanged.bind(item));

        $('#detailsView tbody').append(rowToAppend);
    }

    console.log(JSON.stringify(questionsAndValues)); 


});

0 个答案:

没有答案