简体   繁体   English

克隆的Jquery timePicker

[英]Jquery timePicker for clone

I have a jQuery TimePicker. 我有一个jQuery TimePicker。 It is working fine, but when I clone the row it only works on the first row but not on the cloned rows. 它工作正常,但是当我克隆行时,它只适用于第一行但不适用于克隆行。

This is the code : 这是代码:

<script>
    $(function() {
        $('#timePicker').timepicker({ 'timeFormat': 'H:i' });
    });
</script>       

<input id="timePicker" name = "time[]" type="text" class="time" />

I think it is using the id to call the function. 我认为它使用id来调用函数。 Any other better way ? 还有其他更好的办法?

When you clone the row, initialize also the second timepicker. 克隆行时,也初始化第二个时间戳。

This could create a problem though with the code you have right now because you will have a duplicate id in your DOM. 使用您现在拥有的代码可能会产生问题,因为您的DOM中会有一个重复的ID。 So I suggest you change the id to class and when you duplicate the row you call this: 所以我建议你将id更改为class ,当你复制行时,你调用它:

$('.timePicker').timepicker({ 'timeFormat': 'H:i' });

Or just remove the id of your input field and use this: 或者只删除输入字段的ID并使用:

$('.time').timepicker({ 'timeFormat': 'H:i' });

that is because... when timepicker is called .. the cloned elements is not present in the document thus won't be able to find and add timepicker to it..call timepicker again after the cloned element is appended to the document. 这是因为...当调用timepicker时...克隆元素不存在于文档中,因此无法找到并添加时间戳。在克隆元素附加到文档后再次调用timepicker。

try this 尝试这个

<script>
  $(function() {
    $('.time').timepicker({ 'timeFormat': 'H:i' });
    //-^---here using class selector

    //your codes to append the cloned element.

     $('.time').timepicker({ 'timeFormat': 'H:i' }); //call again
     //-^---here using class selector
 });
</script>

NOTE: make sure you use class beacuse cloning the elements will end up with having two elements with same id which is invalid.. so i am using class here 注意:确保你使用类beacuse克隆元素最终会有两个具有相同id的元素无效..所以我在这里使用类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM