2016-03-29 17 views
-2

これは、行の最初の列にあるチェックボックスをチェックするのに使用するスニペットです。2列を除くすべての行を選択してください

$('[id*=GridView1] tr:not(:first)').each(function (index) { 

    // Selection of entire row 
    $(this).on('click', function (e) { 

     // Check status of checkbox 
     if ($(this).find("input:checkbox").attr('checked')) { 
      $(this).find("input:checkbox").attr('checked', false); 
     } 
     else 
      $(this).find('input:checkbox').attr("checked", true); 
    }); 
}); 

しかし、私はグリッドビューの特定の列(たとえば、最後と3番目の列)でクリック操作を避けたいと考えています。私は "this"要素の選択を適用することができません。いくつかの例を教えていただけますか?お願いします。 あなたは、コールバックの列この方法のインデックスを確認することができ、事前

答えて

0

にありがとう:

$('[id*=GridView1] tr:not(:first)').each(function (index) { 
    $(this).on('click', function (e) { 
     var index = $(event.target).closest("td").index(); 
     // you may now test if index is 3 or the last one 

代替がない行ではなく、細胞に

$('[id*=GridView1] tr:not(:first) td').each(function (index) { 
    var $cell = $(this); 
    $cell.on('click', function (e) { 
     var $row = $cell.closest("tr"); 
をバインドするだろう

大きなテーブルがある場合は、バインディングの数を減らすために委譲を使用することをお勧めします。

0
$('[id*=GridView1] tr:not(:first)').each(function (index) { 
    var row = $(this); 
    var checkbox = row.find("input:checkbox"); 

    row.children('td:not(:last-child)').each(function(ndx) { //skip last cell 
     if(ndx == 2) return true; //skip third cell 

     $(this).on('click', function (e) { 

      // Check status of checkbox 
      if (checkbox.attr('checked')) { 
       checkbox.attr('checked', false); 
      } 
      else 
       checkbox.attr("checked", true); 
     }); 
    }); 
}); 
+0

これは良い解決策です。どうもありがとうございました。 しかし、