getDate with Jquery Datepicker

后端 未结 5 1309
礼貌的吻别
礼貌的吻别 2020-12-05 14:45

i am trying to get date from my implementation of jquery date picker, add it to a string and display the resulting image in my div. Something however is just not working. Ca

相关标签:
5条回答
  • 2020-12-05 15:17

    This line looks questionable:

    page_output.innerHTML = str_output;
    

    You can use .innerHTML within jQuery, or you can use it without, but you have to address the selector semantically one way or the other:

    $('#page_output').innerHTML /* for jQuery */
    document.getElementByID('page_output').innerHTML /* for standard JS */
    

    or better yet

    $('#page_output').html(str_output);
    
    0 讨论(0)
  • 2020-12-05 15:21

    I think you would want to add an 'onSelect' event handler to the initialization of your datepicker so your code gets triggered when the user selects a date. Try it out on jsFiddle

    $(document).ready(function(){
        // Datepicker
        $('#datepicker').datepicker({
            dateFormat: 'yy-mm-dd',
            inline: true,
            minDate: new Date(2010, 1 - 1, 1),
            maxDate:new Date(2010, 12 - 1, 31),
            altField: '#datepicker_value',
            onSelect: function(){
                var day1 = $("#datepicker").datepicker('getDate').getDate();                 
                var month1 = $("#datepicker").datepicker('getDate').getMonth() + 1;             
                var year1 = $("#datepicker").datepicker('getDate').getFullYear();
                var fullDate = year1 + "-" + month1 + "-" + day1;
                var str_output = "<h1><center><img src=\"/images/a" + fullDate +".png\"></center></h1><br/><br>";
                $('#page_output').html(str_output);
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-05 15:22

    You can format the jquery date with this line:

    moment($(elem).datepicker('getDate')).format("YYYY-MM-DD");

    http://momentjs.com

    0 讨论(0)
  • 2020-12-05 15:24

    Instead of parsing day, month and year you can specify date formats directly using datepicker's formatDate function. In my example I am using "yy-mm-dd", but you can use any format of your choice.

    $("#datepicker").datepicker({
        dateFormat: 'yy-mm-dd',
        inline: true,
        minDate: new Date(2010, 1 - 1, 1),
        maxDate: new Date(2010, 12 - 1, 31),
        altField: '#datepicker_value',
        onSelect: function(){
            var fullDate = $.datepicker.formatDate("yy-mm-dd", $(this).datepicker('getDate'));
            var str_output = "<h1><center><img src=\"/images/a" + fullDate +".png\"></center></h1><br/><br>";
            $('#page_output').html(str_output);
        }
    });
    
    0 讨论(0)
  • 2020-12-05 15:34

    try

    $('#page_output').html(str_output);
    
    0 讨论(0)
提交回复
热议问题