Я попытался добавить и удалить строку с выбором даты в таблице. По умолчанию программа выбора даты не работает. Но я нажал кнопку добавления строки, она сработала. Я не мог найти решение.
Пожалуйста, обратитесь к месту Fiddle.
<http://jsfiddle.net/MJGV2/>
Ваш датпикер не работает в первый раз, потому что вы не интуитивно настроили дампикера в первый раз
вот новая скрипка. 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'
});
});
}
Вы должны включить datepicker в DOMready,
Вы можете сделать что-то подобное.
$("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'
});
});
}