Add checkbox dynamically

孤者浪人 提交于 2020-01-16 09:07:39

问题


how to make the checkbox on the table can be checked? I've the code below to add the checkbox dynamically by using a jquery function. The code below succeed to add the checkbox dynamically, but the problem is the checkbox added by the function can't be checked (disabled).

<table id="detail">
  <tr>
    <td><input type="checkbox" id="cb" name="cb[]"></td>
  </tr>
</table>

This is the button to add the row:

<input type="button" id="addRow" value="ADD ROW" />

And this is the jquery function I have:

<script type="text/javascript">
  $(document).ready(function() {
    $("#addRow").click(function() {
      $('#detail tbody>tr:last').clone(true).insertAfter('#detail tbody>tr:last');
    });
  });
</script>

Anyone can help for the code? Thanks... :)


回答1:


Please first correct the javascript function !

  $(document).ready(function() {
    $("#addRow").click(function() {
      $('#detail tbody>tr:last').clone(true).insertAfter('#detail tbody>tr:last');
    });
  });

And your code works for me means the checkbox is checked (only if you tick last checkbox then your code will clone it)

See my jsfiddle or jsfiddle updated




回答2:


Try this

 $(document).ready(function() {
    $("#addRow").click(function() {
      $('#detail tbody tr:last').clone(true).insertAfter('#detail tbody tr:last');
    });
  }​);

check FIDDLE​




回答3:


 $("#addRow").click(function() {
     var row = $('#detail tbody>tr:last').clone(true);

     // Clear last value
     row.find("input:checkbox").attr('checked', false);

     // Change name attr
     row.find("#cb").attr('name', 'newNmae');

     //disabled set as a true
     row.find("input:checkbox").attr('disabled', true);

     row.insertAfter('#detail tbody>tr:last');
});


来源:https://stackoverflow.com/questions/12524799/add-checkbox-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!