none
如何使用Jquery來判斷table中checkbox欄位有無勾選 RRS feed

  • 問題

  • 各位好,請教一個jquery問題

    畫面上輸入查詢條件後,按下"查詢",程式會將查詢結果顯示在GridView裡(有使用分頁,每頁10筆)

    1. 每個Row都有二欄checkbox(使用TemplateField),暫時命名為ck1及ck2
    2. 查詢完成後,使用者才開始勾選畫面上的checkbox
    3. 勾選完成後,按下存檔,此時先再Client端判斷 ck1及ck2的勾選數量,若沒勾選或勾選超過一個以上,則alert訊息(不postback)

    原本我使用javascript已經完成該功能,大致如下:

    function ChkItem()
    {
       
      var ReserveCnt=0;
      var MergeCnt=0;
      var MergeID;
      var _grid = document.getElementById ("ctl00_Content1_GvMerge");
    
      for (i = 1; i <= document.getElementById("HRecordCount").value; i++)
      {            
        if (_grid.rows(i).cells(6).firstChild.checked)
          ReserveCnt += 1;      
        if (_grid.rows(i).cells(7).firstChild.checked)    
          MergeCnt += 1;                
      }
    
         //===ck1數量====
         alert(ReserveCnt );
         //===ck2數量====
         alert(MergeCnt );
    
         ........
         .........
         .........
    

    而檢視原始檔內容如下:

    <tr class="GridDataRowStyle" onclick="select(this)">
     <td>CA0000089</td>
     <td>weili</td>
     <td>1212</td>
     <td>6876454</td>
     <td>547111</td>
     <td>2010/03/11</td>
     <td>
     <input id="ctl00_Content1_GvMerge_ctl02_CkReserve" type="checkbox" name="ctl00$Content1$GvMerge$ctl02$CkReserve" />
     </td>
     <td>
      <input id="ctl00_Content1_GvMerge_ctl02_CkReplace" type="checkbox" name="ctl00$Content1$GvMerge$ctl02$CkReplace" />
     </td>
    </tr>
    

    現在想要利用jquery寫法,來達到同樣效果(取得ck1及ck2勾選的數量)

    try了一些寫法,還沒成功

    // alert($('#ctl00_Content1_GvMerge tr').find("td").eq(i).html());

     //alert($('#ctl00_Content1_GvMerge tr').find("td").eq(i).is(':checked'));

    不知各位先進能否指導一下,非常感謝

    2010年8月23日 上午 11:00

解答

  • 我就是看了你的原文3點才這樣回的,現在看到你的回覆,我更不知道你要什麼了。

     

    是要抓直的嗎? 

    alert($('#ctl00_Content1_GvMerge tr').find("td:eq(6) :checkbox:checked").length);
    alert($('#ctl00_Content1_GvMerge tr').find("td:eq(7) :checkbox:checked").length);

    • 已標示為解答 Tim Hsu 2010年8月24日 上午 05:07
    2010年8月24日 上午 04:55
    版主

所有回覆

  • alert($('#ctl00_Content1_GvMerge :checkbox:checked').length);
    2010年8月23日 上午 11:44
    版主
  • 謝謝您的回覆...

    不過,我想要的值是兩欄checkbox "各別的length"??? (原文3點說明有提到)

    您這個值是加總沒問題,但我該如何指定各別的checkbox

    我再try 看看,還是很謝謝您

     

     

    2010年8月24日 上午 04:28
  • 我就是看了你的原文3點才這樣回的,現在看到你的回覆,我更不知道你要什麼了。

     

    是要抓直的嗎? 

    alert($('#ctl00_Content1_GvMerge tr').find("td:eq(6) :checkbox:checked").length);
    alert($('#ctl00_Content1_GvMerge tr').find("td:eq(7) :checkbox:checked").length);

    • 已標示為解答 Tim Hsu 2010年8月24日 上午 05:07
    2010年8月24日 上午 04:55
    版主
  • 對不起,不好意思,可能是我表達不夠清楚

    是要找直的各別length沒錯

    原本我這樣try不出來


    //alert($('#ctl00_Content1_GvMerge tr').find("td").eq(i).is(':checked'));

    //alert($('#ctl00_Content1_GvMerge:checkbox(0):checked').length);

    剛試了您的寫法是OK的,非常感謝

    2010年8月24日 上午 05:07