2016-04-15 52 views
0

我收到的時候我做檢查的頭複選框問題,下面是我的GridView我綁定在C#中沒有的jQuery阿賈克斯如何計算檢查框(Gridview標題和行中)的值?

第一種方案:如果標題是檢查 - AMT計算是正確的

第二:當取消選中AMT清除正確

第三:當多行復選框人工檢查,即逐個 後來我取消任何一個行,然後我檢查頭複選框,金額 得到calucated是錯的?

我在這個部分有問題在標題Check.Logic失敗。

這裏是我的表像

enter image description here

到目前爲止我所做的是:

$("[id*=chkHeader]").live("click", function() { 
      debugger; 
      var chkHeader = $(this); 
      var grid = $(this).closest("table"); 
      $("input[type=checkbox]", grid).each(function (i) { 
       if (chkHeader.is(":checked")) { 
        $(this).attr("checked", "checked"); 

        if (i >= 1) { 
         if (isNaN($('[id*= txttcfsa]').val()) && isNaN($('[id*= txtcodex]').val())) { 
          $('[id*= txttcfsa]').val(totalPrice.toFixed(2)); 
          $('[id*= txtcodex]').val(totalCodex.toFixed(2)); 
         } else { 
          /* if (grid[0].firstElementChild.childNodes[i].cells[4].firstElementChild.checked != true) {*/ 
           //$('#ctl00_hldPage_txttcfsa').val(0); 
           //$('#ctl00_hldPage_txtcodex').val(0); 

           TCFSA = $('[id*= lbtcfsa]').html(); 
           totalPrice = parseFloat(TCFSA); 
           Codex = $('[id*= lbcodex]').html(); 
           totalCodex = parseFloat(Codex); 
           EarlierTCFSAValue = parseFloat($('[id*= txttcfsa]').val()); 
           EarlierCodexAValue = parseFloat($('[id*= txtcodex]').val()); 

            TCFSTotalBal = parseFloat(EarlierTCFSAValue) + parseFloat(totalPrice); 
            CodexTotalBal = parseFloat(EarlierCodexAValue) + parseFloat(totalCodex); 

           $('[id*= txttcfsa]').val(TCFSTotalBal.toFixed(2)); 
           $('[id*= txtcodex]').val(CodexTotalBal.toFixed(2)); 
          } 
         } 

        /* }*/ 
       } else { 
        $(this).removeAttr("checked"); 


         if (isNaN($('[id*= txttcfsa]').val()) && isNaN($('[id*= txtcodex]').val())) { 
          $('[id*= txttcfsa]').val(totalPrice.toFixed(2)); 
          $('[id*= txtcodex]').val(totalCodex.toFixed(2)); 
         } else { 
          TCFSAAmtRemaning = 0; 
          CodexAmtRemaning = 0; 
          $('[id*= txttcfsa]').val(TCFSAAmtRemaning.toFixed(2)); 
          $('[id*= txtcodex]').val(CodexAmtRemaning.toFixed(2)); 
         } 

       } 
      }); 


     }); 
     $("[id*=chkRow]").live("click", function() { 
      debugger; 
      var grid = $(this).closest("table"); 
      var chkHeader = $("[id*=chkHeader]", grid); 
      if (!$(this).is(":checked")) { 
       chkHeader.removeAttr("checked"); 

       TCFSA = $('[id*= lbtcfsa]').html(); 
       minusprice = parseFloat(TCFSA); 
       Codex = $('[id*= lbcodex]').html(); 
       minusCodex = parseFloat(Codex); 

       if ((isNaN($('[id*= txttcfsa]').val())) && isNaN($('[id*= txtcodex]').val())) { 
        $('[id*= txttcfsa]').val(minusprice.toFixed(2)); 
        $('[id*= txtcodex]').val(minusCodex.toFixed(2)); 

       } 
       else { 
        TCFSAAmtRemaning = parseFloat($('[id*= txttcfsa]').val()); 
        TCFSAAmtRemaning = parseFloat(TCFSAAmtRemaning); 
        CodexAmtRemaning = parseFloat($('[id*= txtcodex]').val()); 
        CodexAmtRemaning = parseFloat(CodexAmtRemaning); 
        TCFSAAmtRemaning = TCFSAAmtRemaning - minusprice; 
        CodexAmtRemaning = CodexAmtRemaning - minusCodex; 

        $('[id*= txttcfsa]').val(TCFSAAmtRemaning.toFixed(2)); 
        $('[id*= txtcodex]').val(CodexAmtRemaning.toFixed(2)); 
       } 


      } else { 
       if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) { 
        chkHeader.attr("checked", "checked"); 

        TCFSA = $('[id*= lbtcfsa]').html(); 
        totalPrice = parseFloat(TCFSA); 
        Codex = $('[id*= lbcodex]').html(); 
        totalCodex = parseFloat(Codex); 
        EarlierTCFSAValue = parseFloat($('[id*= txttcfsa]').val()); 
        EarlierCodexAValue = parseFloat($('[id*= txtcodex]').val()); 
        TCFSTotalBal = parseFloat(EarlierTCFSAValue) + parseFloat(totalPrice); 
        CodexTotalBal = parseFloat(EarlierCodexAValue) + parseFloat(totalCodex); 
        $('[id*= txttcfsa]').val(TCFSTotalBal.toFixed(2)); 
        $('[id*= txtcodex]').val(CodexTotalBal.toFixed(2)); 
       } 
       else { 
        TCFSA = $('[id*= lbtcfsa]').html(); 
        totalPrice = parseFloat(TCFSA); 
        Codex = $('[id*= lbcodex]').html(); 
        totalCodex = parseFloat(Codex); 
        if (isNaN($('[id*= txttcfsa]').val()) && isNaN($('[id*= txtcodex]').val())) { 
         $('[id*= txttcfsa]').val(totalPrice.toFixed(2)); 
         $('[id*= txtcodex]').val(totalCodex.toFixed(2)); 
        } else { 

         TCFSTotalBal = ($('[id*= txttcfsa]').val()); 
         TCFSTotalBal = parseFloat(TCFSTotalBal); 
         CodexTotalBal = parseFloat($('[id*= txtcodex]').val()); 
         CodexTotalBal = parseFloat(CodexTotalBal); 
         TCFSTotalBal = parseFloat(TCFSTotalBal) + parseFloat(totalPrice); 
         CodexTotalBal = CodexTotalBal + totalCodex; 

         $('[id*= txttcfsa]').val(TCFSTotalBal.toFixed(2)); 
         $('[id*= txtcodex]').val(CodexTotalBal.toFixed(2)); 
        } 

       } 
      } 
     }); 

她的是我的aspx設計

<asp:GridView ID ="grd_cfs" runat="server" AutoGenerateColumns="false" > 
<asp:TemplateField HeaderText="TCFSA" ItemStyle-HorizontalAlign="Center" HeaderStyle-CssClass="header-color"> 
            <ItemTemplate> 
             <asp:Label ID="lbtcfsa" runat="server" Text='<%# Eval("TCFSA")%>'></asp:Label> 

            </ItemTemplate> 

           </asp:TemplateField> 
           <asp:TemplateField HeaderText="CODEX" ItemStyle-HorizontalAlign="Center" HeaderStyle-CssClass="header-color"> 
            <ItemTemplate> 
             <asp:Label ID="lbcodex" runat="server" Text='<%# Eval("CODEX")%>'></asp:Label> 

            </ItemTemplate> 
           </asp:TemplateField> 
            <asp:TemplateField HeaderText="Select" HeaderStyle-CssClass="header-color" ItemStyle-HorizontalAlign="Center"> 
                <HeaderTemplate> 
                 <asp:CheckBox ID="chkHeader" runat="server" /> 
                </HeaderTemplate> 
                <ItemTemplate> 
                 <asp:CheckBox ID="chkRow" runat="server" /> 
                </ItemTemplate> 
               </asp:TemplateField> 
</asp:GridView > 

<asp:TextBox ID ="txttcfsa" runat="server" ></asp:TextBox> 
<asp:TextBox ID ="txtcodex" runat="server" > 
+1

請問您可以添加代碼的小提琴,因此很容易調試.. – Bharat

+0

你使用jquery 1.7或更低? – madalinivascu

+0

'if(chkHeader.is(「:checked」)){ $(this).attr(「checked」,「checked」);'你可以解釋一下嗎? – madalinivascu

回答

1

您可以簡化代碼。有兩個事件需要處理。

  1. 當行複選框被改變:找出所有選中的複選框,並從兩列的值相加
  2. 當標題複選框被改變:將所有行復選框頭的狀態,然後觸發行更改事件

這裏是一個正在運行的代碼片段:

//event handler for row clicks 
 
$(".chkRow").on("change", function() { 
 
    var totaltcfsa = 0; 
 
    var totalcodex = 0; 
 

 
    //loop through each checked row and sum the different columns 
 
    $(".chkRow:checked").each(function() { 
 
    var chk = $(this); 
 
    var tcfsa = chk.parent().siblings(":nth-child(3)").text(); 
 
    var codex = chk.parent().siblings(":nth-child(4)").text(); 
 
    totaltcfsa += parseFloat(tcfsa); 
 
    totalcodex += parseFloat(codex); 
 
    }); 
 

 
    //display results 
 
    $("#totaltcfsa").val(totaltcfsa.toFixed(2)); 
 
    $("#totalcodex").val(totalcodex.toFixed(2)); 
 
}); 
 

 
//event handler for header click 
 
$("#chkHeader").on("change", function() { 
 
    var checked = this.checked; 
 

 
    //set all rows to the same as the header 
 
    $(".chkRow").prop("checked", checked); 
 

 
    //trigger row changed event 
 
    $(".chkRow").trigger("change"); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 
<table border="1"> 
 
    <tr> 
 
    <th>CFS Name</th> 
 
    <th>Amount</th> 
 
    <th>TCFSA</th> 
 
    <th>CODEX</th> 
 
    <th><input type="checkbox" id="chkHeader"/></th> 
 
    </tr> 
 
    <tr> 
 
    <td>TRANSWORLD GLS INDIA</td> 
 
    <td>2000.00</td> 
 
    <td>1420.00</td> 
 
    <td>580.00</td> 
 
    <td><input type="checkbox" class="chkRow"></td> 
 
    </tr> 
 
    <tr> 
 
    <td>TRANSWORLD GLS INDIA</td> 
 
    <td>2000.00</td> 
 
    <td>1420.00</td> 
 
    <td>580.00</td> 
 
    <td><input type="checkbox" class="chkRow"></td> 
 
    </tr> 
 
    <tr> 
 
    <td>TRANSWORLD GLS INDIA</td> 
 
    <td>2000.00</td> 
 
    <td>1420.00</td> 
 
    <td>580.00</td> 
 
    <td><input type="checkbox" class="chkRow"></td> 
 
    </tr> 
 
</table> 
 
Total TCFSA <input id="totaltcfsa" /> 
 
Total Codex <input id="totalcodex" />

+0

:您的代碼片段不起作用。請仔細檢查 –

+0

我修復了它,請參閱更新回答。 – dave

+0

YEs @Dave thanks.Upvoted to your answer –

0

首先你必須添加id在每個TD中,並且寫下面查詢,

<script type="text/javascript">  
    $("[id*=chkHeader]").live("click", function() {  
      $("[id*=txttcfsa]").val($(".lbtcfsa", $(this).closest("tr")).html());  
      $("[id*=txtcedex]").val($(".lbcodex", $(this).closest("tr")).html());  
      return false;  
     });  
    </script> 
相關問題