简体   繁体   中英

jquery datepicker code not working (set specific date as default)

I am trying to set the date to a specific one on page load so I'm trying this code:

    <script type="text/javascript">


    $(document).ready(function() { 

  var queryDate = new Date();
queryDate.setFullYear(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});


$("input[type='submit']").click(function(e) {
            e.preventDefault();
            $.post("s.php", $("form").serializeArray(), function(message) {
                window.location="v.php" 
            });
        });
    });

</script>

//The form part where is displays the datepicker:

<form action="#" method="POST">
<div data-role="fieldcontain">
<label for="date">Date:</label>
<input type="date" name="date" id="date" value=""  />
<input type="submit" value="submit" />
 </div>     
</form>

The problem is that nothing's changing ... the default is still the current date :o/

Any ideas anyone please?

UPDATE: Tried this code:

var queryDate = new Date(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});
$('#date').val(queryDate);

And the date is appearing in the input box but the calendar is not in the right month nor date ... Moving foward but still not working.

I'm not sure sure the suggested answer here is not working. As I have tested it display on the right month and year, but only not highlighted the date. This could be because the date format.

Try adding this code to have a default value on your date.

$('#date').val(queryDate);

Then you may see that the format is different with the date in date-picker. If you manage to set the right format with date-picker, you get the things you wanted.

Umm what if you tried this

new Date(year, month, day, hours, minutes, seconds, milliseconds)

var queryDate = new Date();
queryDate.setFullYear(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});

Then the datepicker knows its a date object and how to handle it.


EDIT Ok - all i did was copy the code exactly as you got it here several posts the same suggestion- and the code you edited. Added tags- inlcuded jquery and jquery ui.. I see no problem- the defuatl date is 2009.

</head>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery-ui.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() { 
var queryDate = new Date();
queryDate.setFullYear(2009,11,01);
$('#date').datepicker({defaultDate: queryDate});

$("input[type='submit']").click(function(e) {
        e.preventDefault();
        $.post("s.php", $("form").serializeArray(), function(message) {
            window.location="v.php" 
        });
      });
   });
  </script>

</head>

<body>
//The form part where is displays the datepicker:

<form action="#" method="POST">
<div data-role="fieldcontain">
<label for="date">Date:</label>
<input type="date" name="date" id="date" value=""  />
<input type="submit" value="submit" />

</div>     
</form>

</body>

Result (no styles sheet attached) 在此处输入图片说明

Working example- tell me what is wrong

http://jsfiddle.net/ppumkin/nptgn/

Try this

  var queryDate = new Date(2009,11,01);<br/>
  $('#date').datepicker({defaultDate: queryDate});

我有一个类似的问题,我这样解决了

$('.datepicker').datepicker({dateFormat: "yy-mm-dd", defaultDate: "+3m"} );

Try this

$('#mydate').val("2009-11-01");

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