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

时间:2013-06-25 08:22:23

标签: javascript jquery jquery-ui

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

http://jsfiddle.net/aDxeE/

基本上,一个单独的事件(在构造时不知道)需要在datepicker控件上设置"option","maxDate"。这样可以“清除”真正令人讨厌的文本框值,因为初始构造没有。

有人能提供解决方案吗?

编辑:

将第5行更改为:

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

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

6 个答案:

答案 0 :(得分:2)

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

Fiddle

更改为:

<input id="myinput" type="text" value="01-01-1970" />

//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());

答案 1 :(得分:1)

你可以这样做:

$("#myinput").datepicker('destroy').datepicker({maxDate:new Date()});
  • 完全删除datepicker功能。这将使元素返回其初始化前状态。
  • 再次通过设置maxdate选项重新启动日期选择器。

FIDDLE DEMO

<强>更新

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

$("#myinput").datepicker("destroy");

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

答案 2 :(得分:0)

尝试在$(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"

答案 3 :(得分:0)

如何再次手动设置?

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

答案 4 :(得分:0)

此问题已在此处得到解答:https://stackoverflow.com/a/8945264/2050459

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

以下是使用代码执行此操作的方法:fiddle

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

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

答案 5 :(得分:-1)

Jqueryui DatePicker清除文本框的值?

myinput = ID of datapicker

$("#myinput").val('');