Jquery gridview复选框计算totalminute

时间:2014-05-09 11:44:56

标签: jquery asp.net checkbox

我有一个gridview,数据库中有itime_id,日期和分钟。

我想通过复选框计算选择的总分钟数。我正在使用asp.net和mysql。在这里,当我运行这个网页时,我选中了复选框,我有一个错误,如" NaN"。

     <%@ Page Language="C#" AutoEventWireup="true" CodeFile="hesaplama.aspx.cs"  Inherits="hesaplama" %>

  <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <script type="text/javascript"                                 src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("input[type=checkbox]").change(function () {
   var totalminute = 0, ctlPrice;
 $('#GridView1 tr').each(function () {
 if ($(this).find('input:checkbox').attr("checked")) {
  ctlPrice = $(this).find('[id$= lblListPrice]');
 totalminute += parseInt(ctlPrice.text().replace(/[^\d\.]/g, ''));
   }
                    $("#<%=GridView1.ClientID %> 
           [id*=lblTotal]").text(totalminute.toFixed(2));
         });

     });});


  </script>


  <title></title>
   </head>
   <body>
<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server" ShowFooter="true"   AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="itime_id" HeaderText="itime_id" />

    <asp:TemplateField HeaderText="date">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("date")%>'></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Label ID="Label2" runat="server" Text="Total"></asp:Label>
        </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="minute">
        <ItemTemplate>
            <asp:Label ID="lblListPrice" runat="server" Text='<%#Eval("minute")%>'>          </asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
        </FooterTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" />
        </ItemTemplate>
    </asp:TemplateField>

    </Columns>
    </asp:GridView>
</div>
</form>

protected void Page_Load(object sender, EventArgs e)
{

    MySqlConnection con = new MySqlConnection(Globals.CONNECTION_STRING);
    con.Open();


    string sql = "SELECT * FROM project p,issue_time t";

    MySqlCommand komut = new MySqlCommand(sql, con);
    MySqlDataReader okuyucu = komut.ExecuteReader();
    GridView1.DataSource = okuyucu;
    GridView1.DataBind();

    con.Close();
    con.Dispose();
}

}

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".chkClass").change(function () {
                var total = 0;
                var chks = $(".chkClass input:checked");
                if (chks.length > 0) {
                    for (var i = 0; i < chks.length; i++) {
                        total += parseInt($("#" + chks[i].id.replace("CheckBox1", "lblListPrice")).html());
                    }
                }

                alert("your checked total minutes is :" + total);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
        <Columns>
            <asp:TemplateField HeaderText="minute">
                <ItemTemplate>
                    <asp:Label ID="lblListPrice" runat="server" Text='<%#Eval("Minutes")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" CssClass="chkClass" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>