简体   繁体   中英

Problem with jQuery setdate after upgrade

I have the following code:

<input id="aanschafdatum" type="text" name="aanschafdatum" size="40" value="" />
<input id="sel_aanschafdatum" type="hidden" value="12-05-2011" />

$(document).ready(function() {
    var selected_date = document.getElementById("sel_aanschafdatum").value;
    loadDatepicker('#aanschafdatum', selected_date );
});

 function loadDatepicker(id, selected_date) {
     if (selected_date === undefined) {
         selected_date = "";
     }
     $(function() {
         $(id).datepicker();
     });
     if (selected_date !== '') {
         $(id).datepicker("setDate", selected_date);
     }
 }

This code works fine, the current date "12-05-2011" will be set in the datepicker. But after upgrading to the latest version of jQuery it does not work anymore. Only if I remove the document.ready from:

var selected_date = document.getElementById("sel_aanschafdatum").value;
loadDatepicker('#aanschafdatum', selected_date );

it works.

I have tested it with jsFiddle . With jQuery 1.4.4 it works fine; with jQuery 1.5.2 it does not set the current value.

Hope someone can help, thanks in advance.

Edwin

You have wrong jQuery "syntax", probably in the newer versions they don't forgive such thing anymore.

Inside the function loadDatepicker you nest some code inside $(function() {} block - this is wrong.

It should be the other way around meaning the $(function() {} block should nest functions you want to run. As you already have this code in the context of $(document).ready just remove the $(function() { and you're all good:

function loadDatepicker(id, selected_date) {
     if (selected_date === undefined) {
         selected_date = "";
     }

     $(id).datepicker();

     if (selected_date !== '') {
         $(id).datepicker("setDate", selected_date);
     }
 }

Updated jsFiddle:
http://jsfiddle.net/MgWNv/5/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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