datepicker on dynamically created row with inputs

前端 未结 5 657
暗喜
暗喜 2021-02-08 14:47

I have a form which has the possibility to dynamically create new row with inputs, the date input on each new row should have a datepicker. I have this almost working, but when

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-08 15:52

    In my case, I use clone() to create a copy of datepicker.

    $(".cloneDiv").clone().appendTo("#copyHolder");

    Then I remove the "class" and "id" that datepicker has added to input elements.

    $(".hasDatepicker").removeClass("hasDatepicker").removeAttr("id");

    NOTE: Since the input elements are to be cloned, I don't give them the "id" attribute. So datepicker automatically adds "id" to my DOM element. In addition, if the element to be cloned has user assigned "id" which means there will be at least two elements sharing the same "id", datepicker will have some problem to find the correct one. An example can be found in @j08691 's answer.

    Finally, re-bind datepicker to input elements:

    $(".inputDate").datepicker();

    All the input elements with class "inputDate" will have datepicker.

提交回复
热议问题