【问题标题】:Jquery calculate in tableJquery在表格中计算
【发布时间】:2017-04-26 13:52:30
【问题描述】:

我在表格的行中进行计算。但我有一些问题。我们看下图。

应该是 26.8。每次单击最后一行时,我都会获得该行的数据。

【问题讨论】:

  • 我们应该猜测您代码中的错误吗?或者你会提供代码...
  • 您需要更加具体。您在计算表中的具体内容。你能把一些你认为它不起作用的问题代码贴出来吗?
  • 欢迎来到 SO 。请阅读stackoverflow.com/help/mcve。我们没有水晶球或类似的东西。因此您需要共享所有相关代码并理想地制作一个有效的 sn-p
  • 提示是您的总和等于最后一个值的 n 倍,但正如其他人所说,您必须提供您的代码,以便我们为您提供帮助。

标签: javascript jquery


【解决方案1】:

我昨天忘记添加代码了。对不起

    <script type="text/javascript">

    $(function () {
        $(document).ready(function () {
            $(".AddRow").click(function () {
                var id = $(this).attr("data-id");
                var row = $('#deger tr:last');
                $.ajax({
                    async: true,
                    url: '/Home/Ekle/' + id,
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function (besin) {

                        //I create the rows of the table
                        $("#deger tr:last").before("<tr><td style='height:30px;line-height:30px'>" + besin.BesinAd +
                            "</td><td style='height:30px;line-height:30px'>" + besin.KHDeger + "x" + besin.KHGram +
                            "</td><td style='height:30px;line-height:30px' class='hKh'>" + besin.KHDeger +
                            "</td><td style='height:30px;line-height:30px'  class='hGr'>" + besin.KHGram +
                            "</td><td style='height:30px;line-height:30px'><input type='text' class='form-control input-xs txtHesap'></input></td><td style='height:30px;line-height:30px'><button  class='btn btn-success btn-xs sil'>Sil</button></td></tr>");
                        $(".txtHesap").focus();


                        /*Actions to be performed when a value is entered in the textbox
                        Where only the last added line gets its value. That's my problem.*/

                        $("#deger").on('input', '.txtHesap', function () {
                            var toplam = 0;
                            var khgr = besin.KHGram; //Values ​​from the controller
                            var kh = besin.KHDeger;  //Values ​​from the controller

                            $("#deger .txtHesap").each(function () {
                                var gr = $(this).val();
                                if ($.isNumeric(gr)) {
                                    toplam += parseFloat((gr * kh) / khgr);
                                }
                            });

                            $("#toplam").html(toplam.toFixed(2));
                        });
                    }
                });
            });
        });
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多