2014-11-05 55 views
1

最近我将jquery的datepicker整合到了我的网站中,并且它工作正常。在jquery datepicker的日期按钮上实现url

现在我遇到了一个问题,我找不出解决方案。

我想让datepicker上的日期按钮变成<a></a>标记。这意味着,例如当光标悬停在“2014-09-09”日期上时,URL显示在浏览器的状态栏(左下角)中:mysite.com/look-for-date?date="2014- 09-09“

我可以实现使用Jquery Datepicker吗?如果可能的话,我很想学习如何!谢谢!

+0

不能简单地做我害怕。 Jquery的日期选择器不会派发适当的事件。如果有一个,那么它会被称为'onDateHover'。也许其他一些日期选择器是如此装备。 – 2014-11-05 03:06:04

回答

2

http://jsfiddle.net/k2faz58e/1/

HTML:

<p>Date: <input type="text" id="datepicker"></p> 

JQ:

$.datepicker._updateDatepicker_original = $.datepicker._updateDatepicker; 
    $.datepicker._updateDatepicker = function(inst) { 
     $.datepicker._updateDatepicker_original(inst); 
     var afterShow = this._get(inst, 'afterShow'); 
     if (afterShow) 
      afterShow.apply((inst.input ? inst.input[0] : null)); // trigger custom callback 
    } 

    $("#datepicker").datepicker({ 
     afterShow :function(){ 
     var $dp=$("#ui-datepicker-div"); 

      $dp.find('.ui-state-default').each(function(){ 
       var $td=$(this).parents('td'); 
       var month=parseInt($td.attr('data-month'));     
       month++; 
       var year=$td.attr('data-year') 
       var day=parseInt($(this).text()); 
       if(month<10) month='0'+month; 
       if(day<10) day='0'+day; 
       var date=year+'-'+month+'-'+day; 
       $(this).attr('href','mysite.com/look-for-date?date='+date); 
      }); 

     }, 
    }); 

解决方案2 - 格targtet

新:http://jsfiddle.net/4p0trbz2/

HTML:

<p>Date: <div id="datepicker"></div></p> 

JQ:

$(function() { 

    //http://stackoverflow.com/questions/6334898/jquery-datepicker-after-update-event-or-equivalent 
    $.datepicker._updateDatepicker_original = $.datepicker._updateDatepicker; 
    $.datepicker._updateDatepicker = function(inst) { 
     $.datepicker._updateDatepicker_original(inst); 
     var afterShow = this._get(inst, 'afterShow'); 
     if (afterShow) 
     { 
      afterShow.apply($("#datepicker")[0]); // trigger custom callback 
     } 
    } 

    $("#datepicker").datepicker({ 
     afterShow :function(){ 
     var $dp=$(".ui-datepicker-inline"); 
     $dp.css("display", "block"); 

      $dp.find('.ui-state-default').each(function(){ 
       var $td=$(this).parents('td'); 
       var month=parseInt($td.attr('data-month'));     
       month++; 
       var year=$td.attr('data-year') 
       var day=parseInt($(this).text()); 
       if(month<10) month='0'+month; 
       if(day<10) day='0'+day; 
       var date=year+'-'+month+'-'+day; 
       $(this).attr('href','mysite.com/look-for-date?date='+date); 
      }).click(function(){ 
       window.location=$(this).attr('href'); 
      });   

     }, 
    }); 
}); 
+0

afterShow事件取自http://stackoverflow.com/questions/6334898/jquery-datepicker-after-update-event-or-equivalent – dm4web 2014-11-12 03:44:31

+0

谢谢!但它在这种情况下不起作用:http://jsfiddle.net/k2faz58e/2/ – NeedAnswers 2014-11-12 03:58:34

+0

检查解决方案2与div而不是输入标记 – dm4web 2014-11-12 04:46:18