Remove checkbox attribute in jquery clone

my script code and html looks like this when I try to add a new line, it automatically selects the property of the last flag, if the chekbox is checked, than in the new line add checkbox with the check to check check check check check in new row

script code

<script type="text/javascript">
  $("input.tr_clone_add").live('click', function() {
    var $tr    = $(this).closest('.tr_clone');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
});
</script>  
Table

looks like that:

   <table id="table-data" width="100%">
                  <tr class="tr_clone">
                    <td><input type="checkbox" autofocus name="status[]" value="Y"></td>
                    <td>
                      <select name="group_id[]">
                        <option>Select User</option>
                         <?php
                           $selectGroup = "SELECT  group_id,group_name
                                              FROM `group`";
                           $res = mysql_query($selectGroup);
                           while($row = mysql_fetch_array($res))
                           {
                             echo '<option value="'.$row['group_id'].'">'.$row['group_name'].'</option>';
                           }
                          ?>
                      </select>
                    </td>
                    <td><textarea name="address[]" rows="3" cols="35" placeholder="Enter Address"></textarea></td>
                    <td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
                  </tr>
                </table>
+4
source share
2 answers

check the code below. you can check the box from the cloned element with $clone.find('input[type=checkbox]')and set .attr('checked', false);

works demo

 $("input.tr_clone_add").live('click', function() {
  var $tr    = $(this).closest('.tr_clone');
  var $clone = $tr.clone();
  $clone.find(':text').val('');
  $clone.find(':checked').attr('checked', false);
   // or 
  // $clone.find('input[type=checkbox]').attr('checked', false);
  $tr.after($clone);
});
+2
source

Remove the property checked. Try using

var $clone = $tr.clone().prop('checked', false);

OR

var $clone = $tr.clone().removeAttr('checked');

Fiddle

+1
source

Source: https://habr.com/ru/post/1589187/


All Articles