0

I'm need your helps. I used jquery to create datetime picker, and them work fine, but one problem : When i set value to datetimepicker they set time load the page, when i'm try refresh the value of variable the jquery was crashed. Help please :

There is my code (failed) :

<script type="text/javascript">
var aaa = '{{ now_date|date:'Y-m-d H:i' }}'
$('#datetimepicker').datetimepicker()
        .datetimepicker({value:aaa,step:10});
</script>

P.S. : '{{ now_date|date:'Y-m-d H:i' }}' this is django now time

2 Answers2

1

Since you din't link a reference for jquery datetimepicker(), I assume you're talking about this: http://xdsoft.net/jqplugins/datetimepicker/

In order to accomplish your task, you have to:

  1. be sure that your timepicker starts after document loading using document.ready
  2. you're calling datetimepicker() on $('#datetimepicker'), then you're calling it again on an already jquery/datepicker object, with new parameters. I don't know if it may cause initialization problems, but i can assure you that it's useless.
  3. if you want to change datepicker value on the fly, you have to use it's own method like in this example: http://xdsoft.net/jqplugins/datetimepicker/#runtime_options

here's the code:

<script type="text/javascript">       
    $(document).ready(function(){
        $('#datetimepicker').datetimepicker({
            value : {{ now_date|date:'Y-m-d H:i' }},
            step : 10
        });
    });
    //below, an example of data variable refresh
    $('#datetimepicker').setOptions({value : {{ now_date|date:'Y-m-d H:i' }} });
</script>
pastorello
  • 982
  • 10
  • 23
0

Try this :

$(document).ready(function(){
    $('#datetimepicker').datetimepicker("setDate", aaa);
}

Try this, if this works then try adding other stuffs.

Yunus Aslam
  • 2,447
  • 4
  • 25
  • 39
  • See this http://stackoverflow.com/questions/606463/jquery-datepicker-set-selected-date-on-the-fly – Yunus Aslam Aug 25 '14 at 13:57
  • try simple lile in example, clear field : $('#datetimepicker').datetimepicker("setDate", '{{ now_date|date:'Y-m-d H:i' }}'); or like this $('#datetimepicker').datetimepicker("setDate", new Date(2008,9,03) ); still clear field – user3693234 Aug 25 '14 at 14:04
  • When you click on your text box does it show up the date picker ?? – Yunus Aslam Aug 25 '14 at 14:05
  • What is the ID of your input field in which you are adding datetimepicker ?? – Yunus Aslam Aug 25 '14 at 14:07
  • sorry, i'm not understand – user3693234 Aug 25 '14 at 14:09
  • You are trying to add the datetimepicker to an input text field.. Am I right ?? So what is the ID or class of that input text field ?? Hopefully you must have added ID or class to that field ?? – Yunus Aslam Aug 25 '14 at 14:10
  • if really i'm not understand about what ID you talk, there is the field input : – user3693234 Aug 25 '14 at 14:14
  • id="datetimepicker" this is you ID... So hopefully your code must work. Have you added this inside $(document).ready(function(){}); – Yunus Aslam Aug 25 '14 at 14:15
  • and again sorry, how i can change the value of variable in this example? – user3693234 Aug 25 '14 at 14:17
  • i'm try something id="$(document).ready(function(){$('#datetimepicker').datetimepicker({value:'{{ now_date|date:'Y-m-d H:i' }}',step:10});});" and no change – user3693234 Aug 25 '14 at 14:25
  • Sorry then. I have no other clue. – Yunus Aslam Aug 25 '14 at 14:30
  • id="$(document).ready(function(){$('#datetimepicker').datetimepicker("setDate", '{{ now_date|date:'Y-m-d H:i');}" this don't work's too – user3693234 Aug 25 '14 at 14:30