第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何對這個動態(tài)創(chuàng)建的表中的每一列求和,如 excel

如何對這個動態(tài)創(chuàng)建的表中的每一列求和,如 excel

PHP
達令說 2022-01-14 16:44:46
我會盡力解釋我目前的問題。我創(chuàng)建了一個視圖,類似于 excel。它是動態(tài)創(chuàng)建的。(見下文)  A     B       C| 1|   | 3|   | 7|       // The input format is `<input type='text' class='inputitem' id='colA_row1' />`| 2|   | 6|   | 8|       // The `id` of this `inputitem`is defined by the number of columns and rows automatically| 9|   | 7|   | 4||12|   |16|   |19|       // The format of total textbox is `<input type='text' class='totalitem' id='total_colA' />                                    //// The `id` of this `totalitem` is defined by the number of column automatically用戶可以對any輸入任意數(shù)字,inputitem并將其值totalitem調(diào)整為每列值的總和。(例如,如果用戶將 A 列第 2 行的值更改為 9,則totalcolumnA 列的值將更改為 19)這是我當前的 jquery 代碼:$('.inputitem').on('keyup', function(){        var _inputitem      = $(this);                    var _inputitem_arr  = $(this).attr('id').split('_');        var _inputitem_col  = _inputitem_arr[0];        var _inputitem_row  = _inputitem_arr[1];                    /*SUM SCRIPT*/        var sum_item = 0;        $('.inputitem').each(function(i){            var inputitem_val = parseFloat($(this).val().replace(',', ''));            $('.totalitem').each(function(i){                var _totalitem      = $(this);                var _totalitem_arr  = $(this).attr('id').split('_');                var _totalitem_col  = _totalitem_arr[1];                                                   if(_inputitem_col == _totalitem_col){                        sum_item = sum_item + inputitem_val;                        _totalitem.val(sum_item);                }            });        });             /*END SUM SCRIPT*/});我當前的腳本給出了錯誤的總項目值。似乎將不同列的 SUM 添加到公式中。非常感謝任何幫助和建議
查看完整描述

2 回答

?
縹緲止盈

TA貢獻2041條經(jīng)驗 獲得超4個贊

想想這段代碼的流程。當用戶在頁面上具有“輸入項”類的任何輸入元素上完成按鍵(keyup 事件)時,您的最外層函數(shù)就會執(zhí)行。到現(xiàn)在為止還挺好。


您將總和初始化為 0,然后調(diào)用 $('.inputitem').each(function(i){


此調(diào)用意味著對于頁面上具有“輸入項”類的每個元素,您將在內(nèi)部函數(shù)中運行整個腳本。因此,對于第一個 inputitem(可能是左上角的那個,可能不是),我們在 inputitem_val 中得到值 1.0。


這就是麻煩真正開始的地方。接下來,您為所有單元格調(diào)用 each 函數(shù)。但這是一個嵌套調(diào)用。因此,您正在為外部每個循環(huán)的 9 個(或許多)單元中的每一個重新執(zhí)行最內(nèi)部的功能。這是一個取消嵌套函數(shù)的修復:


$('.inputitem').on('keyup', function(){

    var _inputitem      = $(this);            

    var _inputitem_arr  = $(this).attr('id').split('_');

    var _inputitem_col  = _inputitem_arr[0];


    //whichever column this cell is in is the column we need to re-sum

    var active_col = _inputitem_col



    /*SUM SCRIPT*/

    var sum_item = 0;


    //iterate through each input cell

    $('.inputitem').each(function(i){

        var _inputitem      = $(this);            

        var _inputitem_arr  = $(this).attr('id').split('_');

        var _inputitem_col  = _inputitem_arr[0];


        //check whether the current input cell is in the active column

        if(_inputitem_col == active_col){

            //if so, add it to our partial sum

            var inputitem_val = parseFloat($(this).val().replace(',', ''));

            sum_item += inputitem_val;

        }

    });     


    //find and update only the relavent sum cell

    $('.totalitem').each(function(i){

        var _totalitem      = $(this);

        var _totalitem_arr  = $(this).attr('id').split('_');

        var _totalitem_col  = _totalitem_arr[1];                                   


        if(_inputitem_col == _totalitem_col){

            _totalitem.val(sum_item);

        }

    });

    /*END SUM SCRIPT*/

});


查看完整回答
反對 回復 2022-01-14
?
偶然的你

TA貢獻1841條經(jīng)驗 獲得超3個贊

$('.inputitem').on('keyup', function(){

    var _inputitem_arr  = $(this).attr('id').split('_');

    var _inputitem_col  = _inputitem_arr[0];

    var $totlaSelector  = '#total_' + _inputitem_col;

    var $ColTotal = 0;


    $('[id^="'+_inputitem_col+'"]').each(function(i){

       var $thisVal = 0;

       if($(this).val() != ''){

        $thisVal = parseInt($(this).val());

       }

       $ColTotal = $ColTotal + $thisVal;

    });


    $($totlaSelector).val($ColTotal);

});

我已經(jīng)在 keyup 事件上更新了你的 jQuery。


查看完整回答
反對 回復 2022-01-14
  • 2 回答
  • 0 關(guān)注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號