开发者

jQuery datepicker, 2 questions

开发者 https://www.devze.com 2023-01-22 18:36 出处:网络
For the first question, I have the function: $(function() { $(\'#datepicker\').datepicker({ onSelect: function () {

For the first question, I have the function:

$(function() {
  $('#datepicker').datepicker({
       onSelect: function () {
           $('#date').text($(this).val());
       },
       onChangeMonthYear: function () {
           $('#date').text($(this).val());
      }
  });
});

The onSelect works, but the onChangeMonthYear doesn't. Am I using it wrong?

The second question is, once I get the mont开发者_Go百科h change to work, how can I split up the date so I can put it into a url like "myfile.php?y=2010&m=11&d=01". I have a mainly PHP background, so exploding the string is tempting, but I'm sure theres a better way. Thoughts on that?


According to the documentation for onChangeMonthYear the year, month, and datepicker instance are sent as parameters:

function(year, month, inst)

Allows you to define your own event when the datepicker moves to a new month and/or year. The function receives the selected year, month (1-12), and the datepicker instance as parameters. this refers to the associated input field.

So try using it like this:

$('.selector').datepicker({
   onChangeMonthYear: function(year, month, inst) { ... }
});


With regard to your second question, you can use the getDate API method to extract the date into a date object:

var myDate = jQuery(myDatePicker).datepicker( 'getDate' );

Then you can use the date object methods to extract out the date parts. For example:

var url = "y=" + myDate.getFullYear() +
         "&m=" + myDate.getMonth() + 
         "&d=" + myDate.getDate();


I figured out my own problem with onChangeMonthYear. The way I had it is was calling onSelect each time as well as onChangeMonthYear when the month changed. I rearanged it, and it seems to work fine. The following is the end-result including both Justin's reply, and a guy that deleted his answer:

$(function() {
   $('#datepicker').datepicker({
     onChangeMonthYear: function () {
         $('#date').text("Changed Month");
     },
     onSelect: function () {
         var date = $(this).val().split("/")
         $('#date').text("Month: " + date[0] + " Day: " + date[1] + " Year: " + date[2]);
     }
  });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消