Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript]zmiana css wiersza w tabeli po zaznaczeniu lub odznaczeniu checkboxa
Forum PHP.pl > Forum > Przedszkole
uki8877
Witam

Czy w js mozna cos takiego wykonac ?

zalozmy ze mamy taki kod >

  1. <tr class="">
  2. <td><input type="checkbox" class="checkboxField" ></td>
  3. </tr>
  4. <tr class="">
  5. <td><input type="checkbox" class="checkboxField" ></td>
  6. </tr>
  7. <tr class="">
  8. <td><input type="checkbox" class="checkboxField" ></td>
  9. </tr>
  10. <tr class="">
  11. <td><input type="checkbox" class="checkboxField" ></td>
  12. </tr>


i chcialbym aby po zaznaczeniu checkboxa do wiersza w ktorym znajduje sie ten checkbox zostala dodana jakas klasa. jest to mozliwe ?
mariolita
w JQUERY moze być?

  1. <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
  2.  
  3. <script>
  4.  
  5. $( document ).ready(function() {
  6.  
  7. $(".checkboxField").click( function() {
  8.  
  9. $(this).addClass("innaKLASA");
  10.  
  11. });
  12.  
  13. });
  14.  
  15. </script>


https://jsfiddle.net/a4ojgcm1/
uki8877
moze byc w jQuery, tylko Twoj kod dodaje klase do checkboxa ,a nie do wiersza tabeli
mariolita
https://jsfiddle.net/zocLn58r/

  1. <table id="tabela" border="1" align="center">
  2.  
  3. <tr class="">
  4. <td ><input type="checkbox" class="checkboxField" ></td>
  5. </tr>
  6.  
  7. <tr class="">
  8. <td ><input type="checkbox" class="checkboxField" ></td>
  9. </tr>
  10.  
  11. <tr class="">
  12. <td ><input type="checkbox" class="checkboxField" ></td>
  13. </tr>
  14.  
  15. <tr class="">
  16. <td ><input type="checkbox" class="checkboxField" ></td>
  17. </tr>
  18.  
  19. </table>
  20.  
  21. <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
  22.  
  23. <script>
  24.  
  25. $(document).ready(function(){
  26.  
  27. $(".checkboxField").click(function(){
  28.  
  29. $('#tabela input[type="checkbox"]:checked').each(function(){
  30.  
  31. $(this).parents('tr').addClass("dodanaKlasa");
  32.  
  33. alert($(this).parents('tr').attr("class"));
  34.  
  35. });
  36.  
  37. });
  38.  
  39. });
  40.  
  41. </script>
uki8877
OKI, a jesli chcialbym odwolac sie nie do rodzica (tr) tylko do kolumny (td) o jakiejs klasie ?
czyli mam kod:

<tr class="">
<td class="age">20</td>
<td><input type="checkbox" class="checkboxField" ></td>
</tr>
<tr class="">
<td class="age">21</td>
<td><input type="checkbox" class="checkboxField" ></td>
</tr>

probowalem tak :
  1. alert($this.parent().siblings().find('td .age').text());


jednak alert nic nie wyswietla, konsola rowniez. Jakies pomysly ?

ok ,pomogło ->

  1. alert($this.parent().siblings(".age").text());
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.