admin管理员组

文章数量:1391981

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 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.

Share Improve this question edited May 11, 2016 at 0:11 qasimzee 6501 gold badge12 silver badges30 bronze badges asked May 7, 2011 at 8:32 Satch3000Satch3000 49.5k90 gold badges225 silver badges349 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

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/ppumkin/nptgn/

Try this

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

I was having a similar problem, I solved it like this

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

Try this

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

本文标签: javascriptjquery datepicker code not working (set specific date as default)Stack Overflow