【问题标题】:Jquery gridview checkbox calculate totalminuteJquery gridview复选框计算totalminute
【发布时间】:2014-05-09 20:59:52
【问题描述】:

我有一个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();
}

}

【问题讨论】:

    标签: jquery asp.net checkbox


    【解决方案1】:

    请尝试以下代码 sn-p。

    <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>
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2013-12-17
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      相关资源
      最近更新 更多