开发者

Dynamic JQuery date picker code

I need to create dynamic filter that adds/removes rows dynamically.

It contains a drop-down box. Depending upon the drop-down box value selected, I create a dynamic <TD> that may have a text field or drop-down list.

If it's a text field, then I have to add date picker f开发者_如何学编程or that text field.

I have done this, except date picker for dynamically generated text field.

If you're creating 100 rows, the text fields' names should be same for all rows.

How to add datepicker for dynamically generated text field?


I had the same issue. You would need to rebind the DatePicker to the dynamically added row. The date picker associates a hadDatePicker Class to the dynamic row.

So you would need to remove that and rebind.

Something like this -

    jQuery('.date-pick').removeClass('hasDatepicker').datepicker({
    dateFormat: 'mm-dd-yy'
    });

Regards, Tina Agrawal


Actually i did use the solution provided by @Tina Agrawal But since now, when i select a date from the calendar, i click again and re-select. If i click on next month, it will go to 1900-01-01.

Well it was so strange...

After two hours of trial and errors and research.

i simply did:

$('.date-pick').live('click', function(){
 $('.date-pick').datepicker();
});

Well it works.


I had the same issue and solved it in a different way. Although I am not very sure why is it working as I am very new to jquery. I wrote the following and it iterates the entire set of controls having the class "class_date" and rebinds the datepicker control to it.

$(".class_date").removeClass('hasDatepicker').datepicker();


Tirst add a class attribute as "date" to your input or div.

After dynamically add a text input to have to recall $('.date').datePicker() again to bind datePicker to new inputs or div.


I had a similar problem in that when dynamically adding new rows to my table the Date Picker fields in the new rows (any row added to the DOM dynamically) were all updating my initial rows Date Picker fields.

Removing the hasDatepicker class as suggested above was not enough to solve my issue, probably as I was using .clone() to create my dynamically added rows.

The solution when cloning rows was to remove the cloned input fields, re-create them, add them to my newly cloned table row and THEN re-initiate the Date Picker

For example:

//Remove exisiting cloned input fields
new_row.find("td.date input").remove();

//Create new input fields and append to table td
var date_tds = new_row.find("td.date");
$('<input />', {"name":"gStartDates["+n_array_pos+"]","type":"text"}).appendTo(date_tds[0]);
$('<input />', {"name":"gEndDates["+n_array_pos+"]","type":"text"}).appendTo(date_tds[1]);

//Re-initiate datepicker on the input fields                    
new_row.find("td.date input").datepicker({
    dateFormat:"dd-mm-yy",
    minDate:StartDate,
    maxDate:EndDate
});


Use JQuery Live to access the dynamically created DOM.

http://api.jquery.com/live/

The you can attached the picker.

http://jqueryui.com/demos/datepicker/


one should use ".on" instead of live - see http://api.jquery.com/on/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜