2014-10-02 68 views
0

IM ...jQuery的日期选择在克隆行使用附带的jQuery的日期选择器

new.php

<html> 
<table id="testtable"> 
<tr id="table_row1"> 
<td><input type="text" name="date[]" class="pickDate"></td> 
<td><?php ...some php stuff here...?> </td> 
</tr> 
</table> 
<label onclick="cloneRow('testtable','table_row1')"></label> 
</html> 

的index.php

<html> 
<php 
include('new.php') 
?> 
<script> 
$(document).ready(function() { 
$('.pickDate').each(function() { 
    $(this).datepicker({ dateFormat: 'dd.mm.yy' }); 
}); 
}); 
</script> 
</html> 

的JavaScript功能克隆:

function cloneRow(tablename,rowname) { 
var row = document.getElementById(rowname); // find row to copy 
var table = document.getElementById(tablename); // find table to append to 
var clone = row.cloneNode(true); // copy children too 
clone.id = "newID"; // change id or other attributes/contents 
table.appendChild(clone); // add new row to end of table 
} 

所以问题是,在第一行,日期选择器在那里,并工作,但如果我克隆行,克隆的没有datepicker。

我检查了该类是否被克隆过,是的。

我很新的jquery,但没有jQuery的可能不会注意到,有添加一个新的行?

我如何得到这个工作?

+0

我想你必须在cloneRow函数后再次运行.each()方法。 – Aqib1604 2014-10-02 16:46:34

+0

@Aqib1604怎么样? – 2014-10-02 16:53:29

+0

只是复制并粘贴您的$(“。pickDate”)。每个代码是在document.ready下的cloneRow函数的末尾 – Aqib1604 2014-10-02 16:55:51

回答

1

这里的问题是应用插件的代码只能在页面加载时运行一次。每次克隆节点时都需要应用插件。另外,由于你已经有了jQuery,我修改了代码以使用jQuery来操作元素。

function cloneRow(tablename,rowname) { 
    var row = $("#" + rowname); // find row to copy 
    var table = $("#" + tablename); // find table to append to 
    var clone = row.clone(); // copy children too 
    clone.attr("id", "newID"); // change id or other attributes/contents 
    table.append(clone); // add new row to end of table 
    clone.children(".pickDate").datepicker({ dateFormat: 'dd.mm.yy' }); 
} 
+0

在正常的js文件中做这项工作吗? – 2014-10-02 17:15:21

+0

只要jQuery在被执行之前已经包含在页面中,是的。 jQuery只是一个JavaScript库($实际上只是全局范围内的一个函数),所以你可以像使用其他任何javascript一样使用它。 – Ixonal 2014-10-02 17:32:10