2014-03-29 138 views
0

我试图添加和删除表中的日期选择器的行。默认情况下,日期选择器没有工作。但我点击添加行按钮,它已经工作。我找不到解决方案。日期选择器不工作

请参阅小提琴的位置。

<http://jsfiddle.net/MJGV2/> 

回答

2

您的日期选择在第一次不工作东阳你不拳头时间intilized日期选择器

这里是新的小提琴.. http://jsfiddle.net/MJGV2/2/

// intilize datepicker at document ready or load.. 
$(document).ready(function(){ 

     setdatepicker(); 

}); 

$("input[type='button'].AddRow").live('click', 
function() { 
    var index = $(this).closest('tr').index(); 
    if (index > 0) { 
     $(this).closest('tr').remove(); 

    } else { 


     var $tr = $(this).closest('tr').clone(true); 
     var $input = $tr.find('input.startdatum'); 
     var index = $('input#counter').val(); 
     $('#test').val('Delete'); 
     var id = 'datepicker' + index; 
     index++; 

     $('input#counter').val(index); 
     $input.attr('id', id).data('index', index); 
     console.log(index); 
     $tr.prependTo($(this).closest('table')); 
     setdatepicker(); 

    } 

}); 


function setdatepicker(){ 

    $('.startdatum').each(function() { 
      $(this).datepicker('destroy'); 
      $(this).datepicker({ 
       dateFormat: 'mm-dd-yy' 
      }); 
     }); 
} 
1

您必须启用domready中日期选择器,

你可以做这样的事情。

Fiddle

$("input[type='button'].AddRow").live('click', 
function() { 
var index = $(this).closest('tr').index(); 
if (index > 0) { 
    $(this).closest('tr').remove(); 

} else { 


    var $tr = $(this).closest('tr').clone(true); 
    var $input = $tr.find('input.startdatum'); 
    var index = $('input#counter').val(); 
    $('#test').val('Delete'); 
    var id = 'datepicker' + index; 
    index++; 

    $('input#counter').val(index); 
    $input.attr('id', id).data('index', index); 
    console.log(index); 
    $tr.prependTo($(this).closest('table')); 
    enable_dp(); 

} 

}); 
enable_dp(); 
function enable_dp() 
{ 

    $('.startdatum').each(function() { 
     $(this).datepicker('destroy'); 
     $(this).datepicker({ 
      dateFormat: 'mm-dd-yy' 
     }); 
    }); 
}