2009-08-17 25 views
18

スクリプトで選択した行を強調表示して効果的ですが、行が選択/強調表示されているときに別の行に「非選択/解除」したい行が選択されます。私はこれをどのようにして行うのですか?ここでJquery:クリック時にテーブル行を強調表示/強調表示解除する

は、行を選択するための現在のコードで、それは選択解除が、私は再び同じ行をクリックした場合にのみ:

$(".candidateNameTD").click(function() { 
      $(this).parents("tr").toggleClass("diffColor", this.clicked); 
     }); 

HTMLテーブル

<table id="newCandidatesTable"> 
    <thead> 
     <tr> 
      <th style="cursor: pointer;">ID</th> 
      <th style="cursor: pointer;">Navn</th> 
      <th style="cursor: pointer;">Email</th> 
      <th></th> 
     </tr> 
    </thead> 
    <tbody> 
    <% foreach (var candidate in Model.Ansogninger) 
    { 
     %> 
      <tr id="<%= candidate.AnsogerID %>" class="newCandidatesTableTr"> 
       <td><div id="candidateID"><%= candidate.AnsogerID %></div></td> 
       <td><div id="<%= "candidateName_" + candidate.AnsogerID %>" class="candidateNameTD"><%= candidate.Navn %></div></td> 
       <td><div id="candidateEmail"><%= candidate.Email %></div></td> 
       <td> 
        <div id="<%= "acceptCandidateButton_" + candidate.AnsogerID %>" class="acceptb" style="cursor: pointer; border: 1px solid black; width: 150px; text-align: center;">Godkend</div> 
       </td> 
      </tr> 
     <% 
    } %> 
    </tbody> 
    </table> 

答えて

22

をあなたは最初にすべての選択を解除でき行のように

$(".candidateNameTD").click(function() { 
     $(this).closest("tr").siblings().removeClass("diffColor"); 
     $(this).parents("tr").toggleClass("diffColor", this.clicked); 
    }); 

編集:yep、sry、but thベスト.liveを使用して

$(".candidateNameTD").click(function() { 
    $('table#newCandidatesTable tr').removeClass("diffColor"); 
    $(this).parents("tr").addClass("diffColor"); 
}); 
+0

これにより、クリックされた行の選択が解除されます。 –

+0

ありがとう、私はあなたの考えを使用しました...それは私にとって最も論理的だった:) – Poku

+1

btwネストしたテーブルがあれば、これはすべてのtrに一致します。最も近いのはよかった – redsquare

6
$(".candidateNameTD").click(function() { 
      $("tr").removeClass("diffColor"); 
      $(this).parents("tr").addClass("diffColor"); 
     }); 
4

これは、現在のテーブルに影響します)と、eアイデアは正しかったです。 1つのイベントが多くのものより優先されます(大きなテーブル、大きなオーバーヘッドを考える)。

$("div.candidateNameTD").live('click'. function() { 
    var $tr = $(this).closest("tr"); 
    //remove any selected siblings 
    $tr.siblings().removeClass('diffColor'); 
    //toggle current row 
    $tr.toggleClass('diffColor');   
}); 
0

+0

@redsquare - 彼が動的に行を追加/注入しない限りは必要ありません。 – karim79

+0

彼は多くの行を持っている場合です。 100以上のイベント – redsquare

関連する問題