dongluo9156 2014-12-02 04:46
浏览 405
已采纳

自动计算动态文本框值的总和

I am making an php , mysql website for billing... I want to get the sum of the values in text box.. The rows can be added according to the user requirement... but .. I tried most of things.. but not getting right.... how to get the sum of all the textbox values in the last column rate... I mean without any user input such as button click..... could somebody help me with this code.....

thanks in advance..

code I made for adding rows to table..

         function addRow(tableID) {
        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "text";
        element1.name="s_no[]";
        element1.size="25";
        element1.value = rowCount;
        cell1.appendChild(element1);


        var cell2 = row.insertCell(1);
        var element2 = document.createElement("input");
        element2.type = "text";
        element2.name="p_id[]";
        element2.size="25";
        cell2.appendChild(element2);

        var cell3 = row.insertCell(2);
        var element3 = document.createElement("input");
        element3.type = "text";
        element3.name="p_name[]";
        element3.size="25";
        cell3.appendChild(element3);

        var cell5 = row.insertCell(3);
        var element5 = document.createElement("input");
        element5.type = "text";
        element5.name="mrp[]";
        element5.size="25";
        cell5.appendChild(element5);

        var cell6 = row.insertCell(4);
        var element6 = document.createElement("input");
        element6.type = "text";
        element6.name = "qty[]";
        element6.size="25";
        cell6.appendChild(element6);

        var cell7 = row.insertCell(5);
        var element7 = document.createElement("input");
        element7.type = "text";
        element7.name = "rate[]";
        element7.size="25";
        cell7.appendChild(element7);

        var cell8 = row.insertCell(6);
        var element8 = document.createElement("input");
        element8.type = "checkbox";
        element8.name = "check";
        cell8.appendChild(element8);




}

Html code for table:

    <th width="17%"><label>S.No</label></th>
    <th width="17%">Product Id</th>
    <th width="17%">Product Name</th>
    <th width="17%">MRP</th>
    <th width="17%">Qty</th>
    <th width="18%">Rate</th>
    <th width="18%">Check</th>


</tr>


Total

  • 写回答

4条回答 默认 最新

  • doulangbizhan5160 2014-12-02 05:22
    关注

    HTML:

    <input name="qty[]" type="text" value="0" /><br />
    <input name="qty[]" type="text" value="0" /><br />
    <input name="qty[]" type="text" value="0" /><br />
    <input name="qty[]" type="text" value="0" /><br />
    <input name="qty[]" type="text" value="0" /><br />
    <input name="sum" type="text" value="0" />
    

    Javascript:

    //Get a list of input fields to sum
    var elements = document.getElementsByName("qty[]");
    var element_array = Array.prototype.slice.call(elements);
    
    //Assign the keyup event handler
    for(var i=0; i < element_array.length; i++){
        element_array[i].addEventListener("keyup", sum_values);
    }
    
    //Function to sum the values and assign it to the last input field
    function sum_values(){
        var sum = 0;
        for(var i=0; i < element_array.length; i++){
            sum += parseInt(element_array[i].value, 10);
        }
        document.getElementsByName("sum")[0].value = sum;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA