2011-02-07 112 views
12

我想添加一個「重置」控件到日曆底部的日期選擇器 - 「關閉」控件的位置。這將使用戶能夠將與datepicker綁定的輸入重置爲空(無日期)。JQuery datePicker date reset

我不知道如何編寫綁定函數,具體來說,我該如何獲得綁定到控件的元素的持有?

 if (this.displayClear) { 
      $pop.append(
       $('<a href="#" id="dp-clear">' + $.dpText.TEXT_CLEAR + '</a>') 
       .bind(
        'click', 
        function() 
        { 
          c.clearSelected(); 
          //help! reset the value to '': $(this.something).val('') 
          c._closeCalendar(); 
        } 
       ) 
      ); 
     } 
+0

在[本討論]中有一些有用的答案(http://bugs.jqueryui.com/ticket/3999)。具體請參見[pfurbacher的示例實現](http://jsbin.com/ofare/edit)。 – Ori 2011-02-07 03:49:55

+0

可能的重複[如何清除/重置jQuery UI Datepicker日曆上的選定日期?](http://stackoverflow.com/questions/9435086/how-do-i-clear-reset-the-selected-dates -jquery-ui-datepicker-calendar) – freemanoid 2015-04-10 19:58:19

回答

21

這裏是工作解決方案,只需在任何調用datepicker之前調用cleanDatepicker()。

function cleanDatepicker() { 
    var old_fn = $.datepicker._updateDatepicker; 

    $.datepicker._updateDatepicker = function(inst) { 
     old_fn.call(this, inst); 

     var buttonPane = $(this).datepicker("widget").find(".ui-datepicker-buttonpane"); 

     $("<button type='button' class='ui-datepicker-clean ui-state-default ui-priority-primary ui-corner-all'>Delete</button>").appendTo(buttonPane).click(function(ev) { 
      $.datepicker._clearDate(inst.input); 
     }) ; 
    } 
} 
+1

優秀的解決方案...除了按鈕標題是波蘭語。另外,我建議把它放在一個(function(){})();設置,而不是創建一個命名的函數並稍後調用它。 – Brilliand 2012-03-27 02:34:24

+1

爲了達到此目的,您必須將datepicker選項showButtonPanel設置爲true。 – deerchao 2012-12-07 09:05:56

+0

此解決方案是否適用於AJAX按鈕,也就是說,解決方案是否會導致AJAX以明文方式提交給服務器? – Kawu 2013-01-09 12:16:59

3

我正在經歷這個相同的問題,也就是沒有可用的選項來清空datepicker。後來我發現用甜美的一段代碼,這個超好用發佈註釋here

,你甚至可以在只讀字段添加Clear功能的一個快速方法是將以下代碼添加到您現有的日期選擇器控制:

}).keyup(function(e) { 
    if(e.keyCode == 8 || e.keyCode == 46) { 
     $.datepicker._clearDate(this); 
    } 
}); 

這將使人們突出顯示(他們甚至可以在只讀字段上),然後使用退格或刪除來使用內部_clearDate函數刪除日期。

5
  1. 更改完成標籤,清除並添加代碼以清除

    $(document).ready(function() { 
        $("#startdate").datepicker({ 
         showOn: 'both', 
         buttonImageOnly: true, 
         buttonImage: "css/images/calendar.gif", 
         showButtonPanel: true, 
         closeText: 'Clear', 
         onClose: function (dateText, inst) { 
          $(this).val(''); 
         } 
        }); 
    }); 
    
  2. 添加取消按鈕

    $("#termdate").datepicker({ 
        showOn: 'both', 
        buttonImageOnly: true, 
        buttonImage: "css/images/calendar.gif", 
        showButtonPanel: true, 
        beforeShow: function (input) { 
         setTimeout(function() { 
          var buttonPane = $(input).datepicker("widget") 
           .find(".ui-datepicker-buttonpane"); 
          var btn = $('<button class="ui-datepicker-current' 
           + ' ui-state-default ui-priority-secondary ui-corner-all"' 
           + ' type="button">Clear</button>'); 
          btn.unbind("click").bind("click", function() { 
           $.datepicker._clearDate(input); 
          }); 
          btn.appendTo(buttonPane); 
         }, 1); 
        } 
    }); 
    
13

我找到一個更好的解決方案日曆:

$(document).ready(function() { 
    $(".datepicker").datepicker({ 
      showOn: 'focus', 
      showButtonPanel: true, 
      closeText: 'Clear', // Text to show for "close" button 
      onClose: function() { 
       var event = arguments.callee.caller.caller.arguments[0]; 
       // If "Clear" gets clicked, then really clear it 
       if ($(event.delegateTarget).hasClass('ui-datepicker-close')) { 
        $(this).val(''); 
       } 
      } 
    }); 
}); 

http://jsfiddle.net/swjw5w7q/1/

0

這裏是瓢

我們可以使用默認的完成按鈕綁定一個自定義的清除功能。

$("#date_picker").datepicker({minDate:myDate,dateFormat: 'yy-mm-dd',showButtonPanel:true,closeText:'Clear', 
    beforeShow: function(input) { 
     setTimeout(function() { 
     var clearButton = $(input) 
      .datepicker("widget") 
      .find(".ui-datepicker-close"); 
     clearButton.unbind("click").bind("click",function(){$.datepicker._clearDate(input);}); 
     }, 1); 
    } 
}); 

工程就像一個魅力。乾杯:);)

學分:塞巴斯蒂安Renauld和BehranG比娜

5

這將增加清除按鈕jQuery的日曆,將清除日期。

$("#txtDOJ").datepicker({ 
    showButtonPanel: true, 
    closeText: 'Clear', 
    onClose: function (dateText, inst) { 
     if ($(window.event.srcElement).hasClass('ui-datepicker-close')) { 
      document.getElementById(this.id).value = ''; 
     } 
    } 
});