我正在尝试在按钮点击上添加新行。在我的新行中有一个Textbox,我想用它绑定datepicker但不能。请帮我解决这个问题。
<table>
<tr>
<td><button type="button" onClick ="addRow(this)">Add</button></td>
</tr>
<tr>
<td><input type="text" name="installDate" value="" class ="date"/> </td>
</tr>
</table>
的JavaScript
$(document).on('click', function() {
$('.date').each(function() {
$(this).datepicker();
});
});
function addRow(btn) {
var parentRow = btn.parentNode.parentNode;
var table = parentRow.parentNode;
var rowCount = table.rows.length;
var lastRow = table.rows[rowCount - 1];
var rowClone = lastRow.cloneNode(true);
table.appendChild(rowClone);
$('.date', rowClone).datepicker(); // Code to fix the problem
}
Seq1:添加行=&gt;点击newRow的文本框,弹出日历,一切正常。
SEQ2: 1.单击文档,然后尝试原始行的文本框,然后弹出日历。 2.添加新行。 3.现在点击新行的文本框,日历从不会弹出来选择日期。
附加JSFiddle以供参考 http://jsfiddle.net/wAyhm/9/
答案 0 :(得分:1)
这就是你要找的东西:
How to clone elements that have been bound by a jQuery UI Widget?
工作小提琴:
http://jsfiddle.net/Meligy/DKtA6/6/
window. addRow = function addRow(btn) {
var parentRow = btn.parentNode.parentNode;
var table = parentRow.parentNode;
var rowCount = table.rows.length;
var lastRow = table.rows[rowCount - 1];
var lastDatePicker = $('.date', lastRow);
var rowClone = $(lastRow).clone(true);
var datePickerClone = $('.date', rowClone);
var datePickerCloneId = 'test-id' + rowCount;
datePickerClone.data( "datepicker",
$.extend( true, {}, lastDatePicker.data("datepicker") )
).attr('id', datePickerCloneId);
datePickerClone.data('datepicker').input = datePickerClone;
datePickerClone.data('datepicker').id = datePickerCloneId;
$(table).append(rowClone);
datePickerClone.datepicker();
}