在jqueryui DatePicker上设置’option’会清除文本框的值吗?

我在这里突出显示了这个例子:

http://jsfiddle.net/aDxeE/

基本上,一个单独的事件(在构造时不知道)需要在"option","maxDate"控件上设置"option","maxDate" 。 这“清除”文本框值非常烦人,因为初始构造没有。

谁能提供解决方案?

编辑:

将第5行更改为:

 $("#myinput").datepicker("destroy"); $("#myinput").datepicker({minDate:new Date(), maxDate: new Date()}); 

使它按预期工作,但是是一种严重混乱的方法。

这是格式的问题。 您的初始值dd-mm-yyyy与datepickers默认格式不匹配。 要解决此问题,请将初始值设置为正确的格式,并在创建dateFormat时将格式指定为dateFormat

小提琴

改成:

  

和:

 //construct datepicker $("#myinput").datepicker({minDate:new Date(), dateFormat : "dd-mm-yy"}); //later on, a seperate event changes the maxDate property //but this 'clears' the existing value in the textbox! $("#myinput").datepicker("option","maxDate", new Date()); 

你可以这样做:

 $("#myinput").datepicker('destroy').datepicker({maxDate:new Date()}); 
  • 完全删除datepickerfunction。 这将使元素返回到pre-init状态。
  • 再次使用设置maxdate选项重新启动maxdate

FIDDLE DEMO

UPDATE

为可读性目的编写代码: –

 $("#myinput").datepicker("destroy"); $("#myinput").datepicker({ minDate: new Date(), maxDate: new Date() }); 

尝试在$(document).ready(function(){..});

 //construct datepicker $(document).ready(function(){ $("#myinput").datepicker({minDate:new Date()}); //later on, a seperate event changes the maxDate property //but this 'clears' the existing value in the textbox! }); $("#myinput").datepicker("option","maxDate", new Date()); 

检查JSFiddle 。

仅供参考:使用占位符代替值placeholder="dd-mm-yyyy"

怎么样再次手动设置它?

 var d = $("#myinput").val(); $("#myinput").datepicker("option", "maxDate", "+2d").val(d); 

这个问题已在这里得到解答: https : //stackoverflow.com/a/8945264/2050459

您需要先通过代码设置日期(使用setDate)或打开datepicker(用户交互)。

以下是使用代码进行操作的方法: 小提琴

 $("#myinput").datepicker({ minDate: new Date() }); $("#myinput").datepicker("setDate", new Date()); 

在您的示例中,尝试打开日期选择器,然后触发事件以设置maxDate,您将看到输入将不会被清除,因为已设置了活动日期。

Jqueryui DatePicker清除文本框的值?

 myinput = ID of datapicker $("#myinput").val('');