ASP .NET表行与列的总和与文本框

时间:2015-03-04 16:09:11

标签: javascript c# jquery asp.net webforms

我有一个ASP .NET webforms应用程序,员工正在填写时间表。我有一个包含ASP的表格:TextBox用于用户输入,我想要做的是计算当前行和当前列中小时的总和。

我已经看过前面这个例子了:SUM Total for Column它让我非常接近。我在任何文本框的每个keyup上运行以下javascript:



var totalsByRow = [0, 0, 0, 0, 0];
var totalsByCol = [0, 0, 0, 0, 0];
$(document).ready(function () {
    $('.sum').keyup(function (e) {
        var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");

        $dataRows.each(function (i) {
            $(this).find('td:not(.totalRow)').each(function (j) {
                totalsByCol[j] += parseFloat($(this).val());
                totalsByRow[i] += parseFloat($(this).val());
            });
        });

        for (var i = 0; i < totalsByCol.length - 1; i++) {
            totalsByCol[totalsByCol.length - 1] += totalsByCol[i];
        }

        $("#sum_table td.totalCol").each(function (i) {
            $(this).html("total:" + totalsByCol[i]);
        });

        $("#sum_table td.totalRow").each(function (i) {
            $(this).html("total:" + totalsByRow[i]);
        });
    });
});
&#13;
&#13;
&#13;

问题很简单:$(this).html()没有得到文本框的值。我还尝试了基于其他帖子的$(this).val()和$(this).value但没有成功。那么如何获取调用此keyup函数的文本框的值?

编辑:

我使用的HTMl代码只是一个简单的测试表,我已经设置好了。这是为了进一步澄清:

&#13;
&#13;
<script src="TotalForm.js"></script>
<table id="sum_table" border="1">
  <tr class="titlerow">
    <td>Apple</td>
    <td>Orange</td>
    <td>Watermelon</td>
    <td>Strawberry</td>
    <td>Total By Row</td>
  </tr>
  <tr>
    <td class="rowAA">
      <asp:TextBox runat="server" ID="TextBox1" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowAA">
      <asp:TextBox runat="server" ID="TextBox2" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowBB">
      <asp:TextBox runat="server" ID="TextBox3" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowBB">
      <asp:TextBox runat="server" ID="TextBox4" CssClass="sum"></asp:TextBox>
    </td>
    <td class="totalRow"></td>
  </tr>
  <tr>
    <td class="rowAA">
      <asp:TextBox runat="server" ID="TextBox5" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowAA">
      <asp:TextBox runat="server" ID="TextBox6" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowBB">
      <asp:TextBox runat="server" ID="TextBox7" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowBB">
      <asp:TextBox runat="server" ID="TextBox8" CssClass="sum"></asp:TextBox>
    </td>
    <td class="totalRow"></td>
  </tr>
  <tr>
    <td class="rowAA">
      <asp:TextBox runat="server" ID="TextBox9" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowAA">
      <asp:TextBox runat="server" ID="TextBox10" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowBB">
      <asp:TextBox runat="server" ID="TextBox11" CssClass="sum"></asp:TextBox>
    </td>
    <td class="rowBB">
      <asp:TextBox runat="server" ID="TextBox12" CssClass="sum"></asp:TextBox>
    </td>
    <td class="totalRow"></td>
  </tr>
  <tr class="totalColumn">
    <td class="totalCol">Total:</td>
    <td class="totalCol">Total:</td>
    <td class="totalCol">Total:</td>
    <td class="totalCol">Total:</td>
    <td class="totalCol">Total:</td>
  </tr>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

编辑此部分代码

 $dataRows.each(function (i) {
            $(this).find('td:not(.totalRow)').each(function (j) {
                totalsByCol[j] += parseFloat($(this).val());
                totalsByRow[i] += parseFloat($(this).val());
   });
});

通过

 $dataRows.each(function (i) {
                $(this).find('td:not(.totalRow)').each(function (j) {
                    totalsByCol[j] += parseFloat($(this).find(".sum").val());
                    totalsByRow[i] += parseFloat($(this).find(".sum").val());
       });
    });

.fin(&#34; sum&#34;)找到输入文本控件。