dongshi1424 2015-08-27 11:43
浏览 40
已采纳

无法在表中的克隆行上获取日期选择器

I was trying to get date picker on clone rows in the table.But it is not happening

my table code as follows:

 <input type="text" class="form-control dp4 dob4" autocomplete="off"  name="date[]" required>
 <input type="button" class="btn btn-default addButton" value="Add" />

and javascript for clone row as follows:

 $(function() {
                $("#table-data").on('click', 'input.addButton', function() {
                    var $tr = $(this).closest('tr');
                    var allTrs = $tr.closest('table').find('tr');
                    var lastTr = allTrs[allTrs.length - 1];
                    var $clone = $(lastTr).clone();
                    $clone.find('td').each(function() {
                        var el = $(this).find(':first-child');
                        var id = el.attr('id') || null;
                        if (id) {
                            var i = id.substr(id.length - 1);
                            var prefix = id.substr(0, (id.length - 1));
                            el.attr('id', prefix + (+i + 1));
                            el.attr('name', prefix + (+i + 1));
                        }
                    });
                    $clone.find('input:text').val('');
                    $tr.closest('table').append($clone);
                });

                $("#table-data").on('change', 'select', function() {
                    var val = $(this).val();
                    $(this).closest('tr').find('input:text').val(val);
                });
            });

and my date picker code as follows:

$('.dob4').datepicker({
                    format: 'dd-mm-yyyy',
                    startDate: '-0m',
                    autoclose: true

                });

Please help to solve this issue.

Thanks.

  • 写回答

1条回答 默认 最新

  • douxunnian0423 2015-08-27 12:20
    关注

    There is workaround to it,

    How it Works:

    • Need to remove the class hasDatepickerfrom the cloned elements,because this is what is preventing the datepicker from getting attached to the specific element.
    • Need to remove the id attribute from each of the cloned elements else .datepicker() will assume that datepicker is added to this element.

    • After that call .datepicker() on cloned element.

    JS Code:

     $("#table-data").on('click', 'input.addButton', function () {
         ...
        $clone.find('.dob4').removeAttr('id').removeClass('hasDatepicker');
        $clone.find('.dob4').datepicker({
             format: 'dd-mm-yyyy',
             startDate: '-0m',
             autoclose: true
         });
       ...
     });
    

    Live Demo @ JSFiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?