There is a table of the form

<table class="spc" border="0" cellspacing="0" cellpadding="2" width="98%"> <col width="5%"> <col width="5%"> <col width="10%"> <col width="25%"> <col width="12%"> <col width="15%"> <col width="20%"> <col width="15%"> <tr class="thd "> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ"><input type="checkbox" id="all"></td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">id</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Имя</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Π›ΠΎΠ³ΠΈΠ½</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Π’Π΅Π»Π΅Ρ„ΠΎΠ½</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">ΠŸΠΎΡ‡Ρ‚Π°</td> <td class="thd " onclick="sort(this)"title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">ΠΠΊΡ‚ΠΈΠ²Π½ΠΎΡΡ‚ΡŒ</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Бтатус</td> </tr> <?php foreach ($articles as $data): ?> <tr id="<?=$data['outlookcontacts_id']?>" class="row"> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <input type="checkbox" class="row-select"></td> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <?=$data['outlookcontacts_id']?></td> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <?=$data['Title']?></td> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <?=$data['Middle_Name']?></td> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <?=$data['Last_Name']?></td> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <?=$data['Company']?></td> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <?=$data['E-mail_Address']?></td> <td class="under_cell <?=$data['outlookcontacts_id']?>"> <?=$data['Web_Page']?></td> </tr> <?php endforeach;?> </table> <script type="text/javascript" src="http:/js/table.js"></script> <script type="text/javascript" src="http:/js/modal.js"></script> <script> $(document).ready(function($) { $('.row').click(function(event) { this.style.background='#fffdb5'; if ($(this).find('.row-select').is(":checked")) {$(this).find('.row-select').removeAttr("checked");this.style.background='none';} else {$(this).find('.row-select').attr("checked","checked");} }); }); </script> 

The script, in theory, when you click on a line should mark the checkbox and color the line, and when you re-remove the checkbox and coloring. Once it is possible to do it, but then it only paints the line, and then it stops working.

2 answers 2

In the jQuery documentation, the attr method is not used to change the checked, selected or disabled properties. This is written right at the very beginning of the documentation. To work with them are functions and methods that should be used, in particular prop .

It should also be borne in mind that when an event ascends when it clicks on the checkbox itself, it will change twice, so this moment must also be taken into account.

 $(document).ready(function($) { $('.row').click(function(event) { var row = $(this), chk = $(this).find('.row-select').eq(0); if(!chk.is(event.target)) { chk.prop("checked", !chk.prop("checked")); } if(chk.prop("checked")){ row.css("background","none"); } else { row.css("background","#fffdb5"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="spc" border="0" cellspacing="0" cellpadding="2" width="98%"> <col width="5%"> <col width="5%"> <col width="10%"> <col width="25%"> <col width="12%"> <col width="15%"> <col width="20%"> <col width="15%"> <tr class="thd "> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ"><input type="checkbox" id="all"></td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">id</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Имя</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Π›ΠΎΠ³ΠΈΠ½</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Π’Π΅Π»Π΅Ρ„ΠΎΠ½</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">ΠŸΠΎΡ‡Ρ‚Π°</td> <td class="thd " onclick="sort(this)"title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">ΠΠΊΡ‚ΠΈΠ²Π½ΠΎΡΡ‚ΡŒ</td> <td class="thd " onclick="sort(this)" title="НаТмитС Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ">Бтатус</td> </tr> <tr class="row"> <td class="under_cell"><input type="checkbox" class="row-select"></td> <td class="under_cell">00001</td> <td class="under_cell">title</td> <td class="under_cell">middle name</td> <td class="under_cell">last name</td> <td class="under_cell">company</td> <td class="under_cell">E-mail address</td> <td class="under_cell">web page</td> </tr> <tr class="row"> <td class="under_cell"><input type="checkbox" class="row-select"></td> <td class="under_cell">00002</td> <td class="under_cell">title</td> <td class="under_cell">middle name</td> <td class="under_cell">last name</td> <td class="under_cell">company</td> <td class="under_cell">E-mail address</td> <td class="under_cell">web page</td> </tr> </table> 

     $('.row').click(function(event) { $(this).find('.row-select').click(); this.style.background = ''; if ($(this).find('.row-select').is(":checked")) { this.style.background = '#ccc'; } }); }); 
    • one
      And after you changed the color by clicking on the line, does the checkbox itself fall off? - Alex Krass