2017-03-16 13 views

回答

1

嘗試這個請運行該代碼段

您需要使用.closest()

.closest(): -對於組中的每一個元素,得到第一個元素通過測試元素本身並遍歷DOM樹中的祖先來匹配選擇器。從當前元素開始。向上行進,DOM樹,直到找到一個匹配的供選擇

$('.clsChk').click(function(){ 
 
    if($(this).prop('checked')){ 
 
\t \t \t $(this).closest('tr.rowClass').css({'color':'red'}); 
 
    } 
 
    else{ 
 
\t \t $(this).closest('tr.rowClass').css({'color':'black'}); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
<label for="checkboxID"> Del/Undelete </label> 
 
<input type="checkbox" id="checkboxID" /> 
 

 
<table id="tableID"> 
 
    <tbody> 
 
     <tr class="rowClass"> 
 
      <td>** 1) First child cell of '.rowClass'</td> 
 
      <td>** 2) Second child cell of '.rowClass' </td> 
 
      <td>** 3) this checkbox needs a delete 
 
       <input class='clsChk' type="checkbox" id="checkboxID" /> 
 
      </td> <!--how to make this work--> 
 
     </tr> 
 
     
 
     <tr class="rowClass"> 
 
      <td>No class for this row</td> 
 
      <td>(Still no class)</td> 
 
     </tr> 
 
     <tr class="rowClass"> 
 
      <td>** 1) First child cell of '.rowClass'</td> 
 
      <td>** 2) Second child cell of '.rowClass' </td> 
 
      <td>** 3) this checkbox needs a delete 
 
       <input class='clsChk' type="checkbox" id="checkboxID" /> 
 
      </td> <!--how to make this work--> 
 
     </tr> 
 
     <tr class="rowClass"> 
 
      <td>** 1) First child cell of '.rowClass'</td> 
 
      <td>** 2) Second child cell of '.rowClass' </td> 
 
      <td>** 3) this checkbox needs a delete 
 
       <input class='clsChk' type="checkbox" id="checkboxID" /> 
 
      </td> <!--how to make this work--> 
 
     </tr> 
 
    </tbody> 
 
</table>

enter image description here

+0

@transformer - 見上面的代碼和示例代碼 – prog1011

+0

你好,這是偉大的,我想知道,如果有喜歡的dropdownlists等其他控制......將這些控件也。也改變顏色。我只想改變文本的顏色,並在開關/取消刪除 - 將其恢復到以前的狀態 – transformer

+0

@transformer這隻會改變文本顏色。它不會更改任何HTML控件的顏色。 如果這個答案可以幫助你,然後請投票並打勾作爲答案......所以它會幫助其他人找出正確的答案。 – prog1011

1

使用parentsis(":checked")託運與否。

$('input:checkbox').change(function(){ 
 
    if($(this).is(":checked")) { 
 
     $(this).parents('tr').addClass("red"); 
 
    } else { 
 
     $(this).parents('tr').removeClass("red"); 
 
    } 
 
});
.red { 
 
    
 
    color : red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="tableID"> 
 
    <tbody> 
 
     <tr class="rowClass"> 
 
      <td>** 1) First child cell of '.rowClass'</td> 
 
      <td>** 2) Second child cell of '.rowClass' </td> 
 
      <td>** 3) this checkbox needs a delete <input type="checkbox" id="checkboxID" /></td> <!--how to make this work--> 
 
     </tr> 
 
     
 
     <tr> 
 
      <td>No class for this row</td> 
 
      <td>(Still no class)</td> 
 
     </tr> 
 
    </tbody> 
 
</table>

+0

嗨,在m html片段中...我在​​裏面有另一個輸入文本框,用'class = textBox' ---我*更新的* html代碼片段爲' ​​** 1).rowClass'***添加了'FYI - 自*我有其他控件,如下拉列表以及* ...在桌子裏面 – transformer

+0

有沒有其他的js你在你的博客上做的東西 – transformer